22. 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);
}
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.
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
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 "";
}
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())
);
46. 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