KEMBAR78
Introduction to Spring Boot | PDF
Introduction to Spring Boot
Trey Howard
@thoward333
thoward333
https://www.linkedin.com/in/treyhoward
Why Spring?
● Dependency Injection
○ Promotes more maintainable code
○ Promotes testable code
● First class support for all layers of the stack
○ Tomcat, Jetty, Undertow
○ View: JSP, Velocity, Freemarker, JSON, REST/SOAP
○ Repository: Hibernate, JPA, JDBC
○ Database: RDBMS and NoSQL
○ Testing: JUnit, Mockito, Spring Test
Why Spring Boot?
● More choices = More complexity
● Inconsistent dependency versions
● @Autoconfiguration
● Maven or Gradle
● Convention over Configuration
○ application.properties (and -profile.properties)
○ src/main/resources: /templates, /static
● Harmonizes library versions (Spring and non-Spring)
○ Version of Spring Boot is the only thing you have to manage
● Opinionated
○ Embedded Tomcat for MVC app, 1-line config change to Jetty, Undertow or none
○ Detects Spring Security on classpath and wires up sensible defaults
■ You don’t want Spring Security? 1-line change to turn off
■ Don’t like the OOTB settings? Extend this class
● spring-boot-starter-actuator
Benefits of Spring Boot
Creating a Spring Boot Project
● Create a seed project using web, IDE, or CLI
○ http://start.spring.io/
○ Download Spring Source STS (Latest 3.8.1)
○ Install CLI (Latest 1.4.0)
■ Creates, runs & more
● Secret sauce: Parent pom.xml
● Include spring-boot-starter-* modules
Spring Boot CLI
Entire project is single file: hello.groovy
@RestController
class WebApplication {
@RequestMapping("/")
String home() {
"Hello World!"
}
}
spring run hello.groovy
Live Demo - Create New Boot Project
http://start.spring.io/
Demo - REST API
● Basic REST API
● Hard-coded repository class for now
Demo - JDBC
● spring-boot-starter-jdbc
● Uses JdbcTemplate to query database
Demo - JPA
● spring-boot-starter-data-jpa
● Uses CrudRepository to query database
● Notice it’s an interface, not a class
● Method names drive fields that are queried
○ findByEmail(String email) will query db with ‘...where email = ?’
Demo - JPA Data REST
● spring-boot-starter-data-jpa & spring-boot-starter-data-rest
● No @Controller
● Because we have included spring-boot-starter-data-rest, Spring creates
REST endpoints for all @Entity classes
Spring Security with Spring Boot
● spring-boot-starter-security
● Add @EnableWebSecurity
OAuth2 with Spring Boot
● Add @EnableAuthorizationServer
○ Creates /oath/token endpoint that creates OAuth tokens
● Add @EnableResourceServer
○ Receives OAuth token via HTTP Header
● Demo
○ curl localhost:8080/oauth/token -d scope=read -d grant_type=password -d
username=springboot -d password=workshop -u myid:mysecret
○ curl localhost:8080 -H “Authorization: Bearer <access_token>”
Goodies in Spring Web (Boot 1.4.0)
● spring-devtools: Dual Classpath and Live Reload
● URL Binding annotations:
○ @GetMapping, @PostMapping, etc (@RequestMapping pre-1.4.0)
○ @RestControllerAdvice (@ControllerAdvice + @ResponseBody pre-1.4.0)
○ @SpringBootTest, @WebMvcTest, @AutoConfigureMockMvc, @MockBean, @SpyBean
● HTTP Options, Head are now supported
● RestTemplate setDefaultUriVariables
● @ConfigurationProperties / @EnableConfigurationProperties
○ Strongly typed properties backed by property files
○ Optional metadata for IDE, extra devtools module will auto-generate metadata
Goodies in Spring Security (Boot 1.4.0)
● Can access Principal in SpEL expression in Repository layer @Query:
@Query(“select * from foo where owner = ?#{principal.userId}”)
public Foo getFooForCurrentUser() ...
● mvcMatcher - alternative to antMatcher, has suffix vulnerability /foo vs /foo/
● @EnableAuthorizationServer / @EnableResourceServer
Resources
● Spring Boot Reference Guide:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
○ Installing Spring Boot CLI:
http://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-installing-spring-bo
ot.html#getting-started-installing-the-cli
● Initializr: http://start.spring.io/
● What’s new in Spring Boot 1.4:
https://spring.io/blog/2016/07/28/spring-boot-1-4-released
Introduction to Spring Boot

Introduction to Spring Boot

  • 1.
    Introduction to SpringBoot Trey Howard @thoward333 thoward333 https://www.linkedin.com/in/treyhoward
  • 2.
    Why Spring? ● DependencyInjection ○ Promotes more maintainable code ○ Promotes testable code ● First class support for all layers of the stack ○ Tomcat, Jetty, Undertow ○ View: JSP, Velocity, Freemarker, JSON, REST/SOAP ○ Repository: Hibernate, JPA, JDBC ○ Database: RDBMS and NoSQL ○ Testing: JUnit, Mockito, Spring Test
  • 3.
    Why Spring Boot? ●More choices = More complexity ● Inconsistent dependency versions ● @Autoconfiguration
  • 4.
    ● Maven orGradle ● Convention over Configuration ○ application.properties (and -profile.properties) ○ src/main/resources: /templates, /static ● Harmonizes library versions (Spring and non-Spring) ○ Version of Spring Boot is the only thing you have to manage ● Opinionated ○ Embedded Tomcat for MVC app, 1-line config change to Jetty, Undertow or none ○ Detects Spring Security on classpath and wires up sensible defaults ■ You don’t want Spring Security? 1-line change to turn off ■ Don’t like the OOTB settings? Extend this class ● spring-boot-starter-actuator Benefits of Spring Boot
  • 5.
    Creating a SpringBoot Project ● Create a seed project using web, IDE, or CLI ○ http://start.spring.io/ ○ Download Spring Source STS (Latest 3.8.1) ○ Install CLI (Latest 1.4.0) ■ Creates, runs & more ● Secret sauce: Parent pom.xml ● Include spring-boot-starter-* modules
  • 6.
    Spring Boot CLI Entireproject is single file: hello.groovy @RestController class WebApplication { @RequestMapping("/") String home() { "Hello World!" } } spring run hello.groovy
  • 7.
    Live Demo -Create New Boot Project http://start.spring.io/
  • 8.
    Demo - RESTAPI ● Basic REST API ● Hard-coded repository class for now
  • 9.
    Demo - JDBC ●spring-boot-starter-jdbc ● Uses JdbcTemplate to query database
  • 10.
    Demo - JPA ●spring-boot-starter-data-jpa ● Uses CrudRepository to query database ● Notice it’s an interface, not a class ● Method names drive fields that are queried ○ findByEmail(String email) will query db with ‘...where email = ?’
  • 11.
    Demo - JPAData REST ● spring-boot-starter-data-jpa & spring-boot-starter-data-rest ● No @Controller ● Because we have included spring-boot-starter-data-rest, Spring creates REST endpoints for all @Entity classes
  • 12.
    Spring Security withSpring Boot ● spring-boot-starter-security ● Add @EnableWebSecurity
  • 13.
    OAuth2 with SpringBoot ● Add @EnableAuthorizationServer ○ Creates /oath/token endpoint that creates OAuth tokens ● Add @EnableResourceServer ○ Receives OAuth token via HTTP Header ● Demo ○ curl localhost:8080/oauth/token -d scope=read -d grant_type=password -d username=springboot -d password=workshop -u myid:mysecret ○ curl localhost:8080 -H “Authorization: Bearer <access_token>”
  • 14.
    Goodies in SpringWeb (Boot 1.4.0) ● spring-devtools: Dual Classpath and Live Reload ● URL Binding annotations: ○ @GetMapping, @PostMapping, etc (@RequestMapping pre-1.4.0) ○ @RestControllerAdvice (@ControllerAdvice + @ResponseBody pre-1.4.0) ○ @SpringBootTest, @WebMvcTest, @AutoConfigureMockMvc, @MockBean, @SpyBean ● HTTP Options, Head are now supported ● RestTemplate setDefaultUriVariables ● @ConfigurationProperties / @EnableConfigurationProperties ○ Strongly typed properties backed by property files ○ Optional metadata for IDE, extra devtools module will auto-generate metadata
  • 15.
    Goodies in SpringSecurity (Boot 1.4.0) ● Can access Principal in SpEL expression in Repository layer @Query: @Query(“select * from foo where owner = ?#{principal.userId}”) public Foo getFooForCurrentUser() ... ● mvcMatcher - alternative to antMatcher, has suffix vulnerability /foo vs /foo/ ● @EnableAuthorizationServer / @EnableResourceServer
  • 16.
    Resources ● Spring BootReference Guide: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/ ○ Installing Spring Boot CLI: http://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-installing-spring-bo ot.html#getting-started-installing-the-cli ● Initializr: http://start.spring.io/ ● What’s new in Spring Boot 1.4: https://spring.io/blog/2016/07/28/spring-boot-1-4-released