Buzzworthy Java - Spring Boot & JHipster

Jeff Sheets
Nebraska.Code() Conf - 2015

These slides are up on GitHub

jeffsheets.github.io/BuzzworthyJava2015

Also a printable version | Open Printable PDF

http://projects.spring.io/spring-boot/
http://jhipster.github.io/
https://twitter.com/java_hipster/status/522857424181809152

Buzzworthy Objectives

  • An introduction to both Spring Boot and JHipster
  • An overview of the technologies involved
  • Sample Code
  • Pragmatic Usage Ideas
  •  
  • Please ask questions! Open discussion
  • Everyone in this room is an expert!

About Me - Jeff Sheets

  • Java/Grails/Javascript for Object Partners
  •  @sheetsj
  • Website  sheetsj.com
  • jeffsheets@gmail.com
  • Omaha

Object Partners Inc (OPI)

  • Java, Groovy, JavaScript, Mobile, Open Source
  • ~100 Senior Consultants
    • Minneapolis, Omaha
    • Chicago, Denver
    • Average Tenure Over 5 years
  • Founded 1996

What's Cool in 2015?

  • JavaScript
  • JavaScript
  • JavaScript
  • Groovy, Clojure, Scala
  • F#, Swift, Go, Haskell, Ruby
  • ??? Java ???

Set the record straight

  • We learned Java because it was the 'hipster' lang
  • C and COBOL were the old domain
  • Garbage Collection and Object Oriented FTW!
http://jhipster.github.io/

Java's Image Problem

  • NodeJS, Ruby, Go, Swift, etc
  • Code Verbosity and Ceremony
  • Too "enterprisey"
  • App Servers
http://www.buzzfeed.com/awesomer/the-most-hipster-things-that-have-ever-happened

JVM Languages Help

  • Groovy was Swift before Swift
  • Nashorn runs JS on the JVM
  • JRuby for Ruby
  • Clojure, Scala, Kotlin

Java's Speed Problem

  • Not app performance
    • Java wins that battle (see Netflix)
    • "Velocity on the JVM is the Killer App" - Andy Glover
  • But speed-to-market
    • Hard to jumpstart
    • XML everywhere
    • App Servers

Spicing up Java's Image

  • Spring Boot
    • Write an app in a tweet! (so hipster)
    • Opinionated Spring all wired up
    • Production-ready by default
    • No App Server Required - executable Jars
  • JHipster
    • Full stack AngularJS + Spring Boot
    • JS Build Tools
    • Beautiful Metrics
    • JHipster-loaded
  • Groovy Ecosystem
    • Groovy / Spock / Gradle / gvm / Grails
  • Others - Dropwizard, Akka, Vert.x

Spring Boot!  

  • Opinionated Spring all wired up
  • 100% Java Config (no XML!)
  • Executable Jars, embedded web server (or not)
  • Sane Profiles and Properties
  • Easy REST & Microservices
  • Spring Data JPA - dynamic finders (grails style)
  • Production-ready by default

http://wiimedia.ign.com/wii/image/article/104/1045630/new-super-mario-bros-wii-20091116030234930.jpg

Boot Install and CLI

  • 'curl -s get.gvmtool.net | bash' (for gvm)
  • gvm install springboot
  • A full tweet-sized 140 character app:
  • spring run GBR.groovy
  • @RestController
      class GBR {
          @RequestMapping("/")
          String gbr() {
              "Go Big Red! #Nebrasketball"
          }
      }
    

Jumpstart a Real Boot App

  • start.spring.io INITIALIZR
  • a la carte selection menu
  • IntelliJ will soon natively support

Or just 'curl start.spring.io'

https://twitter.com/craigburke1/status/575690058667286528

Embedded Web Container

  • Just run as a main class in a jar
  • Great for cloud deploys. Microservices. Fat Jars.
  • Weblogic, Websphere, JBoss, etc NOT REQUIRED
  • gradle bootRun
  • java -jar appname.jar
  • Or run Main class from IDE

Or generate a War

  • gradle war
  • mvn package (also an executable jar file)
  • Warning Disclaimer:
    • Running latest of any Java stack on Websphere/Weblogic is tricky. Especially Hibernate/JPA.
    • Because classloaders...
    • It can be done
    • Get your Google / Stackoverflow search game right

Java Config

  • No XML, all Java Config (unless you like XML)
  • @Configuration
      @EnableAutoConfiguration
      @ComponentScan
      public class Application {
      
          public static void main(String[] args) {
              SpringApplication.run(Application.class, args);
          }
          
      }
    

Everything is all wired up

  • No fiddling with hooking it all together
  • Production-ready out of the box

Profiles

  • Important piece of Spring Boot
  • dev/test/prod profiles
  • Available in core Spring too, but Boot leans on them

Properties

  • Hierarchical properties
  • application-*.properties, application.properties, System Props, JNDI Var, JVM Arg, ...

Spring Data JPA

  • Simple CRUD Repositories
  • (findAll, create, update, delete, and more)
  • Dynamic Finders
public interface AttendeeRepository extends JpaRepository {

    List findByOrderByNameDesc();
    
    List findByNameOrderByNameAsc(String name);
    
    List findByOrderByNameAsc();
}

Spring Data REST

  • Easy REST Entities
  • HATEOAS enabled
  • Better for quick prototypes than prod (customization)
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
  public interface PersonRepository extends PagingAndSortingRepository {
  
    List findByLastName(@Param("name") String name);
    
  }

Spring MVC Spock Tests  

class AccountControllerTest extends Specification {
  def accountController = new AccountController()
  
  MockMvc mockMvc = standaloneSetup(accountController).build()
  
  def "getAccount test hits the URL and parses JSON output"() {
      when: 'rest account url is hit'
      def response = mockMvc.perform(get('/rest/account')).andReturn().response
      def content = new JsonSlurper().parseText(response.contentAsString)
      
      then: 'securityService correctly returned account in JSON'
      response.status == OK.value()
      
      //Can test the whole content string that is returned
      response.contentAsString == '{"username":"spockUser"}'
      
      //Or can use the JsonSlurper version to test the JSON as object
      content.username == 'spockUser'
  }
}

Much more

  • JSP/GSP/Thymeleaf
  • Metrics - app-server style
  • No-SQL data stores, if that's your thing
  • Great docs
  • Simple tutorials

Old + New = Spring Boot

http://www.buzzfeed.com/awesomer/the-most-hipster-things-that-have-ever-happened

JHipster!  

  • Opinionated AngularJS + Spring Boot
  • Yeoman, Grunt, Bower, Gradle
  • Karma Client-side tests
  • Liquibase data bootstrapping
  • Thymeleaf, Hazelcast, Logback, HikariCP
  • Websockets with Atmosphere

Community & Branding

  • It starts with an image
  • Plus, the developers are amazing people
  • The project is fun and positive
  • Exhibit A: Response to Comic-Sans bug
  • Julien Dubois @juliendubois
  • Jérôme Mirc @JeromeMirc

Code Generation?

  • Yeoman generates a jumpstarted app
  • Also Grails-like entity generators
  • Personally, I use it to build up an app structure
  • At a minimum, generate an app and borrow ideas
  • Can 'update' from JHipster updates, but YMMV
  • vs Maven Archetypes

Slides!

  • So cool they provide their own slides
  • and they're much cooler than mine
  • so I borrowed the next few slides from them
  • Launch

JHipster Goals

  • A beautiful front-end
  • Latest HTML5/CSS3/JavaScript frameworks
  • Latest Java/Caching/Data access technologies
  • With security and performance in mind
  • And great developer tooling

High Level

Generate App

Steps
npm install -g yo
npm install -g generator-jhipster
yo jhipster
gradle bootRun
grunt server

Ship It!

http://i.giphy.com/FPt4fI9BvUOXu.gif

Code Walk-thru

Entity Generators

Create a Meeting Attendee List
yo jhipster:entity location
//city, state, one-to-many attendee

yo jhipster:entity attendee
//name, previousAttendee, speaker, notes, many-to-one location

Show and Tell

  • Change db to on-disk
  • Display city, state on attendees.html list (Livereload!)
  • Improve previousAttendee and speaker Y/N
  • BrowserSync

JHipster-loaded

  • Hot Swap almost any class
  • Stop bouncing your app server
  • Like JRebel but free! (as in speech AND beer)
  • Extension on Spring-loaded (used by Grails)

Dev and Prod profiles

  • Dev by default
  • Compile or run with -Pprod to use Prod mode
  • or an exectuable war:
mvn -Pprod package
java -jar jhipster-0.0.1-SNAPSHOT.war --spring.profiles.active=prod

Very Actively Developed

  • 2.6.0 added full stack Validations
  • 2.5 added Gatling Performance testing
  • Spring Boot 1.2.2 added JMustache support

Extras

  • Liquibase
  • Metrics screen
  • GZipping and HTTP Caching Headers
  • ehcache and hazelcast
  • Docker container
  • Heroku and Openshift deployments

Conclusion

All good? What's not to like?

  • Bloat - Downloads the whole internet
  • Memory footprint is tough for free cloud deploys
  • Treat container support as "beta" (my words)
    • I had trouble with Websphere 8.5.5

Should I use this in my legacy application?

YES!

http://i.giphy.com/8CSflsMG1IFos.gif

In your legacy app:

  • Pull in pieces today
  • Look at github for Spring Boot
  • Generate a JHipster app and look around
  • Use Spring Profiles now!
  • Java Config!
  • Like building Grails from scratch...

Many Many Thanks!

  • Bootiful Apps talk by Josh Long
  • Exploring Microframeworks by Dan Woods
  • JHipster Twitter account @java_hipster

Thank You!

 @sheetsj

http://i.giphy.com/UPXy00gJYEdgc.gif