© Copyright 2017 Pivotal Software, Inc. All rights Reserved. Version 1.0
Jay Lee(jaylee@pivotal.io)
Younjin Jeong(yjeong@pivotal.io)
November 2017
Spring 5 New Features
https://spring.io/blog/2016/09/22/new-in-spring-5-functional-web-framework
https://content.pivotal.io/white-papers/speed-thrills-how-to-harness-the-power-of-ci-cd-for-your-development-team
Discount Code: S1P200_Jeong
https://pivotal.io/training/courses
http://engineering.pivotal.io/
https://spring.io/blog/
https://repo.spring.io/webapp/#/home
JDK 9, HTTP/2, Reactive
https://docs.spring.io/spring/docs/current/spring-framework-reference/
Spring 5 Major Changes
! Java 9 Compatible
! JavaEE8 Support
! HTTP/2 Support
! Reactive: WebFlux, Router Functions
! Functional Bean Configuration
! JUnit 5
! Portlet Deprecated
Baseline Changes
Version Upgrades introduced in Spring 5
!JDK 8+
!Servlet 3.1+
!JMS 2.0
!JPA 2.1
!JavaEE7+
Baseline Changes – Java9 Jigsaw
SPR-13501: Declare Spring modules with JDK 9 module metadata
is still Open
Manifest-Version: 1.0
Implementation-Title: spring-core
Automatic-Module-Name: spring.core
Implementation-Version: 5.0.2.BUILD-SNAPSHOT
Created-By: 1.8.0_144 (Oracle Corporation)
JavaEE 8 GA – Sep 21, 2017
! CDI 2.0 (JSR 365)
! JSON-B 1.0 (JSR 367)
! Servlet 4.0 (JSR 369)
! JAX-RS 2.1 (JSR 370)
! JSF 2.3 (JSR 372)
! JSON-P 1.1 (JSR 374)
! Security 1.0 (JSR 375)
! Bean Validation 2.0 (JSR 380)
! JPA 2.2 - Maintenance Release
Java API for JSON Binding (JSON-B) 1.0 – JSR 367
Standard Binding Layer for converting Java objects to/from JSON
! Thread Safe
! Apache Johnzon
! Eclipse Yasson
Java API for JSON Binding (JSON-B) 1.0 – JSR 367
Person person = new Person();

person.name = ”Jay Lee";

person.age = 1;

person.email = “jaylee@pivotal.io”;



Jsonb jsonb = JsonbBuilder.create();

String JsonToString = jsonb.toJson(person);



System.out.println(JsonToString );



person = jsonb.fromJson("{"name":”Jay Lee","age":1,”email":”jaylee@pivotal.io"}",
Person.class);
Java API for JSON Binding (JSON-B) 1.0 – JSR 367
<dependency>

<groupId>org.apache.geronimo.specs</groupId>

<artifactId>geronimo-json_1.1_spec</artifactId>

<version>1.0</version>

</dependency>

<dependency>

<groupId>org.apache.johnzon</groupId>

<artifactId>johnzon-jsonb</artifactId>

<version>1.1.5</version>

</dependency>
Java API for JSON Binding (JSON-B) 1.0 – JSR 367
<dependency>

<groupId>org.glassfish</groupId>

<artifactId>javax.json</artifactId>

<version>1.1.2</version>

</dependency>

<dependency>

<groupId>org.eclipse</groupId>

<artifactId>yasson</artifactId>

<version>1.0.1</version>

</dependency>
Java API for JSON Binding (JSON-B) 1.0 – JSR 367
New HttpMessageConverter for JSON-B in Spring5
@Bean

public HttpMessageConverters customConverters() {

Collection<HttpMessageConverter<?>> messageConverters = new ArrayList<>();

JsonbHttpMessageConverter jsonbHttpMessageConverter = new JsonbHttpMessageConverter();

messageConverters.add(jsonbHttpMessageConverter);



return new HttpMessageConverters(true, messageConverters);

}
Servlet 4.0 – Server Push
Server pushes resources to Clients, efficient way to transfer
resources using HTTP/2
@GetMapping("/pushbuilder")

public String getPage(PushBuilder builder) {

builder.path("/scripts.js").push();

builder.path("/styles.css").push();



return "index";

}
Servlet 4.0 – HttpServletMapping
Provide Runtime Discovery of URL Mappings
@GetMapping("/servlet4")

public void index(final HttpServletRequest request) {

HttpServletMapping mapping = request.getHttpServletMapping();


System.out.println(mapping.getServletName());

System.out.println(mapping.getPattern());

System.out.println(mapping.getMappingMatch().name());

System.out.println(mapping.getMatchValue());

}
Bean Validation 2.0 – JSR 380
1.1 was introduced in 2013, lacking of supports for new Java8, 9.
! New JDK Types Including LocalTime, Optional and etc
! Lambda
! Type Annotation
! @Email, @Positive, @PositiveOrZero, @Negative, @NegativeOrZero,
@PastOrPresent, @FutureOrPresent, @NotEmpty, and @NotBlank.
Spring 5 Major Changes
! Logging Enhancement
! Performance Enhancement
! Functional Bean Configuration
! JSR 305
! Spring MVC - HTTP/2 Support,
! Reactive: WebFlux, Router Functions
! JUnit 5
Spring 5 – XML Configuration Changes
Streamlined to use unversioned Schema
Spring 5 - Logging Enhancement
spring-jcl replaces Commons Logging by default
! Autodetecting Log4j 2.x, SLF4J, and JUL (java.util.logging) by
Class Loading
Spring 5 - Logging Enhancement
Spring 5 - Component Scanning Enhancement
Component scanning time reduced by index, improving Start Up Time
• META-INF/spring.components is created at Compile Time
• @Indexed Annotation
• org.springframework.context.index.CandidateComponentsIndex
• -Dspring.index.ignore=true to fall back to old mechanism
@Target({ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface Component {

String value() default "";

}
@Target({ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Indexed

public @interface Component {

String value() default "";

}
Spring 5 - Component Scanning Enhancement
<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-indexer</artifactId>

<optional>true</optional>

</dependency>
Spring 5 - Component Scanning Enhancement
• JMH Result, ClassPath Scanning versus Index Scanning
• Noticeable Differences in having more classes
https://github.com/snicoll-scratches/test-spring-components-index
Spring 5 - Functional Bean Configuration
Support for Functional Bean Registration in
GenericApplicationContext, AnnotationConfigApplicationContext
! Very efficient, no reflection, no CGLIB proxies involved
! Lambda with Supplier act as a FactoryBean
Spring 5 - Functional Bean Configuration
@Autowired

GenericApplicationContext ctx;



@Test

public void functionalBeanTest() {

ctx.registerBean(Person.class, () -> new Person());

ctx.registerBean("personService", Person.class,

() -> new Person(), bd -> bd.setAutowireCandidate(false));

ctx.registerBean(”carService", Car.class,

() -> new Car(), bd -> bd.setScope(BeanDefinition.SCOPE_PROTOTYPE));

}
Spring 5 – JSR 305
SPR-15540 Introduce null-safety of Spring Framework API
• Becomes Kotlin Friendly (-Xjsr305=strict as of 1.1.51)
https://github.com/spring-projects/spring-framework/commit/f813712f5b413b354560cd7cc006352e9defa9a3
Spring 5 – JSR 305 Nullable
New Annotation org.springframework.lang.NonNull,
org.springframework.lang.Nullable leverages JSR 305
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Nonnull

@TypeQualifierNickname

public @interface NonNull {

}
Spring 5 – Spring MVC
HTTP/2 and Servlet 4.0
• PushBuilder
• WebFlux
• Support Reactor 3.1, RxJava 1.3, 2.1 as return values
• JSON BINDING API
• Jackson 2.9
• Protobuf 3.0
• URL Matching
Spring 5 – HTTP/2 Support Containers
! Tomcat 9.0
! Jetty 9.3
! Undertow 1.4
ex. Spring Boot
<dependency>

<groupId>org.apache.tomcat</groupId>

<artifactId>tomcat-juli</artifactId>

<version>9.0.1</version>

</dependency>
Spring 5 MVC – Multipart File Size
MaxUploadSizeExceededException will be thrown for multipart
size overrun
• Default Value
• Two Properties(Default Value)
• spring.servlet.multipart.max-file-size=1MB

spring.servlet.multipart.max-request-size=10MB
Spring 5 MVC – URL Matcher changed
PathPatternParser is alternative to Traditional AntPathMatcher for
URL Matching
• org.springframework.web.util.patterns.PathPattern
Examples:
/pages/t?st.html —/pages/test.html, /pages/tast.html  /pages/toast.html
/resources/*.png — matches all .png files in the resources directory
/resources/** — matches all files underneath the /resources/ path, including /resources/
image.png and /resources/css/spring.css
/resources/{*path} — matches all files underneath the /resources/ path and captures their relative
path in a variable named "path"; /resources/image.png will match with ”path" -> "/image.png", and /
resources/css/spring.css will match with ”path" -> "/css/spring.css"
/resources/{filename:w+}.dat will match /resources/spring.dat and assign the value "spring" to
the filename variable
Spring 5 MVC – Throwing Exception from Controller
ResponseStatusException is Introduced to throw Exception in
MVC
• SPR-14895:Allow HTTP status exceptions to be easily thrown from
Controllers
• ResponseEntity(HttpStatus.BAD_REQUEST)?
@GetMapping ( "/throw" )

public void getException () {

throw new ResponseStatusException( HttpStatus. BAD_REQUEST , "request invalid." ); 

}
Spring 5 WebFlux – WebClient
Reactive Web Client introduced in Spring 5, alternative to
RestTemplate
! AsyncRestTemplate is deprecated
WebClient client= WebClient.create();



Mono<Person> person=client

.get()

.uri("http://jay-person.cfapps.io/{name}",name)

.retrieve()

.bodyToMono(Person.class);
Spring 5 WebFlux –WebTestClient
@RunWith(SpringRunner.class)

@SpringBootTest

@AutoConfigureWebTestClient

public class MicrometerApplicationTests {

@Autowired

WebTestClient client;



@Test

public void testWebClient() {

client.get().uri("/person/Jay")

.exchange()

.expectBody(Person.class)

.consumeWith(result -> assertThat(result.getResponseBody().getName()).isEqualTo("Jay Lee"));

}
Spring 5 WebFlux – Server
RouterFunction is introduced for functional programming
RouterFunction<?> route = route(GET("/users"), request ->

ok().body(repository.findAll(), Person.class))

.andRoute(GET("/users/{id}"), request ->

ok().body(repository.findOne(request.pathVariable("id")), Person.class))

.andRoute(POST("/users"), request ->

ok().build(repository.save(request.bodyToMono(Person.class)).then())

);
WebFlux server (functional)
RouterFunction<?> route = route(GET("/users"), request ->
ok().body(repository.findAll(), User.class))
.andRoute(GET("/users/{id}"), request ->
ok().body(repository.findOne(request.pathVariable("id")), User.class))
.andRoute(POST("/users"), request ->
ok().build(repository.save(request.bodyToMono(User.class)).then())
);
Spring Boot and Cloud
Spring Boot 2.0 will be GA in Feb 2018
! Spring Cloud Finchley Release Train based on Spring Boot 2.0
! Spring Cloud Gateway
! Spring Cloud Function
Spring Calendar
https://spring-calendar.cfapps.io/
Spring 5 – Deprecated APIs
HTTP/2 and Servlet 4.0
• Hibernate 3, 4
• Portlet
• Velocity
• JasperReports
• XMLBeans
• JDO
• Guava
https://run.pivotal.io https://pcfdev.io
Transforming How The World Builds Software
© Copyright 2017 Pivotal Software, Inc. All rights Reserved.

Spring5 New Features - Nov, 2017

  • 1.
    © Copyright 2017Pivotal Software, Inc. All rights Reserved. Version 1.0 Jay Lee(jaylee@pivotal.io) Younjin Jeong(yjeong@pivotal.io) November 2017 Spring 5 New Features
  • 2.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
    Spring 5 MajorChanges ! Java 9 Compatible ! JavaEE8 Support ! HTTP/2 Support ! Reactive: WebFlux, Router Functions ! Functional Bean Configuration ! JUnit 5 ! Portlet Deprecated
  • 15.
    Baseline Changes Version Upgradesintroduced in Spring 5 !JDK 8+ !Servlet 3.1+ !JMS 2.0 !JPA 2.1 !JavaEE7+
  • 16.
    Baseline Changes –Java9 Jigsaw SPR-13501: Declare Spring modules with JDK 9 module metadata is still Open Manifest-Version: 1.0 Implementation-Title: spring-core Automatic-Module-Name: spring.core Implementation-Version: 5.0.2.BUILD-SNAPSHOT Created-By: 1.8.0_144 (Oracle Corporation)
  • 17.
    JavaEE 8 GA– Sep 21, 2017 ! CDI 2.0 (JSR 365) ! JSON-B 1.0 (JSR 367) ! Servlet 4.0 (JSR 369) ! JAX-RS 2.1 (JSR 370) ! JSF 2.3 (JSR 372) ! JSON-P 1.1 (JSR 374) ! Security 1.0 (JSR 375) ! Bean Validation 2.0 (JSR 380) ! JPA 2.2 - Maintenance Release
  • 18.
    Java API forJSON Binding (JSON-B) 1.0 – JSR 367 Standard Binding Layer for converting Java objects to/from JSON ! Thread Safe ! Apache Johnzon ! Eclipse Yasson
  • 19.
    Java API forJSON Binding (JSON-B) 1.0 – JSR 367 Person person = new Person();
 person.name = ”Jay Lee";
 person.age = 1;
 person.email = “jaylee@pivotal.io”;
 
 Jsonb jsonb = JsonbBuilder.create();
 String JsonToString = jsonb.toJson(person);
 
 System.out.println(JsonToString );
 
 person = jsonb.fromJson("{"name":”Jay Lee","age":1,”email":”jaylee@pivotal.io"}", Person.class);
  • 20.
    Java API forJSON Binding (JSON-B) 1.0 – JSR 367 <dependency>
 <groupId>org.apache.geronimo.specs</groupId>
 <artifactId>geronimo-json_1.1_spec</artifactId>
 <version>1.0</version>
 </dependency>
 <dependency>
 <groupId>org.apache.johnzon</groupId>
 <artifactId>johnzon-jsonb</artifactId>
 <version>1.1.5</version>
 </dependency>
  • 21.
    Java API forJSON Binding (JSON-B) 1.0 – JSR 367 <dependency>
 <groupId>org.glassfish</groupId>
 <artifactId>javax.json</artifactId>
 <version>1.1.2</version>
 </dependency>
 <dependency>
 <groupId>org.eclipse</groupId>
 <artifactId>yasson</artifactId>
 <version>1.0.1</version>
 </dependency>
  • 22.
    Java API forJSON Binding (JSON-B) 1.0 – JSR 367 New HttpMessageConverter for JSON-B in Spring5 @Bean
 public HttpMessageConverters customConverters() {
 Collection<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
 JsonbHttpMessageConverter jsonbHttpMessageConverter = new JsonbHttpMessageConverter();
 messageConverters.add(jsonbHttpMessageConverter);
 
 return new HttpMessageConverters(true, messageConverters);
 }
  • 23.
    Servlet 4.0 –Server Push Server pushes resources to Clients, efficient way to transfer resources using HTTP/2 @GetMapping("/pushbuilder")
 public String getPage(PushBuilder builder) {
 builder.path("/scripts.js").push();
 builder.path("/styles.css").push();
 
 return "index";
 }
  • 24.
    Servlet 4.0 –HttpServletMapping Provide Runtime Discovery of URL Mappings @GetMapping("/servlet4")
 public void index(final HttpServletRequest request) {
 HttpServletMapping mapping = request.getHttpServletMapping(); 
 System.out.println(mapping.getServletName());
 System.out.println(mapping.getPattern());
 System.out.println(mapping.getMappingMatch().name());
 System.out.println(mapping.getMatchValue());
 }
  • 25.
    Bean Validation 2.0– JSR 380 1.1 was introduced in 2013, lacking of supports for new Java8, 9. ! New JDK Types Including LocalTime, Optional and etc ! Lambda ! Type Annotation ! @Email, @Positive, @PositiveOrZero, @Negative, @NegativeOrZero, @PastOrPresent, @FutureOrPresent, @NotEmpty, and @NotBlank.
  • 26.
    Spring 5 MajorChanges ! Logging Enhancement ! Performance Enhancement ! Functional Bean Configuration ! JSR 305 ! Spring MVC - HTTP/2 Support, ! Reactive: WebFlux, Router Functions ! JUnit 5
  • 27.
    Spring 5 –XML Configuration Changes Streamlined to use unversioned Schema
  • 28.
    Spring 5 -Logging Enhancement spring-jcl replaces Commons Logging by default ! Autodetecting Log4j 2.x, SLF4J, and JUL (java.util.logging) by Class Loading
  • 29.
    Spring 5 -Logging Enhancement
  • 30.
    Spring 5 -Component Scanning Enhancement Component scanning time reduced by index, improving Start Up Time • META-INF/spring.components is created at Compile Time • @Indexed Annotation • org.springframework.context.index.CandidateComponentsIndex • -Dspring.index.ignore=true to fall back to old mechanism @Target({ElementType.TYPE})
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 public @interface Component {
 String value() default "";
 } @Target({ElementType.TYPE})
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 @Indexed
 public @interface Component {
 String value() default "";
 }
  • 31.
    Spring 5 -Component Scanning Enhancement <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context-indexer</artifactId>
 <optional>true</optional>
 </dependency>
  • 32.
    Spring 5 -Component Scanning Enhancement • JMH Result, ClassPath Scanning versus Index Scanning • Noticeable Differences in having more classes https://github.com/snicoll-scratches/test-spring-components-index
  • 33.
    Spring 5 -Functional Bean Configuration Support for Functional Bean Registration in GenericApplicationContext, AnnotationConfigApplicationContext ! Very efficient, no reflection, no CGLIB proxies involved ! Lambda with Supplier act as a FactoryBean
  • 34.
    Spring 5 -Functional Bean Configuration @Autowired
 GenericApplicationContext ctx;
 
 @Test
 public void functionalBeanTest() {
 ctx.registerBean(Person.class, () -> new Person());
 ctx.registerBean("personService", Person.class,
 () -> new Person(), bd -> bd.setAutowireCandidate(false));
 ctx.registerBean(”carService", Car.class,
 () -> new Car(), bd -> bd.setScope(BeanDefinition.SCOPE_PROTOTYPE));
 }
  • 35.
    Spring 5 –JSR 305 SPR-15540 Introduce null-safety of Spring Framework API • Becomes Kotlin Friendly (-Xjsr305=strict as of 1.1.51) https://github.com/spring-projects/spring-framework/commit/f813712f5b413b354560cd7cc006352e9defa9a3
  • 36.
    Spring 5 –JSR 305 Nullable New Annotation org.springframework.lang.NonNull, org.springframework.lang.Nullable leverages JSR 305 @Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 @Nonnull
 @TypeQualifierNickname
 public @interface NonNull {
 }
  • 37.
    Spring 5 –Spring MVC HTTP/2 and Servlet 4.0 • PushBuilder • WebFlux • Support Reactor 3.1, RxJava 1.3, 2.1 as return values • JSON BINDING API • Jackson 2.9 • Protobuf 3.0 • URL Matching
  • 38.
    Spring 5 –HTTP/2 Support Containers ! Tomcat 9.0 ! Jetty 9.3 ! Undertow 1.4 ex. Spring Boot <dependency>
 <groupId>org.apache.tomcat</groupId>
 <artifactId>tomcat-juli</artifactId>
 <version>9.0.1</version>
 </dependency>
  • 39.
    Spring 5 MVC– Multipart File Size MaxUploadSizeExceededException will be thrown for multipart size overrun • Default Value • Two Properties(Default Value) • spring.servlet.multipart.max-file-size=1MB
 spring.servlet.multipart.max-request-size=10MB
  • 40.
    Spring 5 MVC– URL Matcher changed PathPatternParser is alternative to Traditional AntPathMatcher for URL Matching • org.springframework.web.util.patterns.PathPattern Examples: /pages/t?st.html —/pages/test.html, /pages/tast.html  /pages/toast.html /resources/*.png — matches all .png files in the resources directory /resources/** — matches all files underneath the /resources/ path, including /resources/ image.png and /resources/css/spring.css /resources/{*path} — matches all files underneath the /resources/ path and captures their relative path in a variable named "path"; /resources/image.png will match with ”path" -> "/image.png", and / resources/css/spring.css will match with ”path" -> "/css/spring.css" /resources/{filename:w+}.dat will match /resources/spring.dat and assign the value "spring" to the filename variable
  • 41.
    Spring 5 MVC– Throwing Exception from Controller ResponseStatusException is Introduced to throw Exception in MVC • SPR-14895:Allow HTTP status exceptions to be easily thrown from Controllers • ResponseEntity(HttpStatus.BAD_REQUEST)? @GetMapping ( "/throw" )
 public void getException () {
 throw new ResponseStatusException( HttpStatus. BAD_REQUEST , "request invalid." ); 
 }
  • 42.
    Spring 5 WebFlux– WebClient Reactive Web Client introduced in Spring 5, alternative to RestTemplate ! AsyncRestTemplate is deprecated WebClient client= WebClient.create();
 
 Mono<Person> person=client
 .get()
 .uri("http://jay-person.cfapps.io/{name}",name)
 .retrieve()
 .bodyToMono(Person.class);
  • 43.
    Spring 5 WebFlux–WebTestClient @RunWith(SpringRunner.class)
 @SpringBootTest
 @AutoConfigureWebTestClient
 public class MicrometerApplicationTests {
 @Autowired
 WebTestClient client;
 
 @Test
 public void testWebClient() {
 client.get().uri("/person/Jay")
 .exchange()
 .expectBody(Person.class)
 .consumeWith(result -> assertThat(result.getResponseBody().getName()).isEqualTo("Jay Lee"));
 }
  • 44.
    Spring 5 WebFlux– Server RouterFunction is introduced for functional programming RouterFunction<?> route = route(GET("/users"), request ->
 ok().body(repository.findAll(), Person.class))
 .andRoute(GET("/users/{id}"), request ->
 ok().body(repository.findOne(request.pathVariable("id")), Person.class))
 .andRoute(POST("/users"), request ->
 ok().build(repository.save(request.bodyToMono(Person.class)).then())
 );
  • 45.
    WebFlux server (functional) RouterFunction<?>route = route(GET("/users"), request -> ok().body(repository.findAll(), User.class)) .andRoute(GET("/users/{id}"), request -> ok().body(repository.findOne(request.pathVariable("id")), User.class)) .andRoute(POST("/users"), request -> ok().build(repository.save(request.bodyToMono(User.class)).then()) );
  • 46.
    Spring Boot andCloud Spring Boot 2.0 will be GA in Feb 2018 ! Spring Cloud Finchley Release Train based on Spring Boot 2.0 ! Spring Cloud Gateway ! Spring Cloud Function
  • 47.
  • 48.
    Spring 5 –Deprecated APIs HTTP/2 and Servlet 4.0 • Hibernate 3, 4 • Portlet • Velocity • JasperReports • XMLBeans • JDO • Guava
  • 49.
  • 50.
    Transforming How TheWorld Builds Software © Copyright 2017 Pivotal Software, Inc. All rights Reserved.