Spring MVC 4.2: New and Noteworthy

64
SPRINGONE2GX WASHINGTON, DC Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring MVC 4.2: New and Noteworthy Rossen Stoyanchev

Transcript of Spring MVC 4.2: New and Noteworthy

Page 1: Spring MVC 4.2: New and Noteworthy

SPRINGONE2GXWASHINGTON, DC

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Spring MVC 4.2:New and Noteworthy

Rossen Stoyanchev

Page 2: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

About Me

• Spring Framework committer

• Spring MVC

• Spring WebSocket

2

Page 3: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Demo Source

3

https://github.com/rstoyanchev/s2gx2015-spring-mvc-42

Page 4: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

HTTP Streaming

4

Page 5: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Spring 3.2: Async Request Handling

• The ability to get off the Servlet container thread

• Take any controller method • replace return value T with DeferredResult<T>

• Range of use cases• long polling -- chat, live events• processing with latency -- scatter-gather, microservices

• Easy to introduce into existing apps

5

Page 6: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Spring 4.0: WebSocket Messaging

• Full duplex messaging with SockJS emulation

• STOMP for application messaging• simple or external broker• @MessagingMapping methods for application work

• More advanced messaging needs• collaboration, games

• Very different from REST -- messaging architecture

6

Page 7: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

• Return ResponseBodyEmitter rather than @ResponseBody

• Or ResponseEntity<ResponseBodyEmitter>

• Stream of serialized Objects, emitted from any thread

• Objects serialized with HttpMessageConverter

Spring 4.2: HTTP Streaming

7

Page 8: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Example@RequestMapping(path = "/stream", method = GET)public ResponseBodyEmitter startStream() { ResponseBodyEmitter emitter = new ResponseBodyEmitter();

... return emitter;}

// Later on emitter.send(foo, MediaType.APPLICATION_JSON);...emitter.send(bar, MediaType.APPLICATION_JSON);... emitter.complete();

8

MediaType usedto select a converter

Page 9: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

• Store ResponseBodyEmitter instances• in controller• in another component where events received

• Use onCompletion callback (on emitter) to remove instance

• Manual pub-sub to connected users

• Compare to STOMP + WebSocket• messaging protocol with built-in pub-sub

Pub-Sub

9

Page 10: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

• Return SseEmitter, an extension of ResponseBodyEmitter

• HTTP streaming with event markup

Spring 4.2: Server-Sent Events

10

Page 11: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Example@RequestMapping(path = "/stream", method = GET)public SseEmitter startStream() { SseEmitter emitter = new SseEmitter();

... return emitter;}

// Later on emitter.send(event().event().id("id").name("type").data(foo));...emitter.complete();

11

Page 12: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Spring 4.2: Direct Streaming• No serialization with HttpMessageConverter

• Stream directly to the response

• Return StreamingResponseBody instead of @ResponseBody

• ResponseEntity<StreamingResponseBody>

12

Page 13: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Example@RequestMapping(path = "/stream", method = GET)public StreamingResponseBody handle() {

return new StreamingResponseBody() {

@Override public void writeTo(OutputStream os) throws IOException { os.write(...); os.flush(); } };}

13

Page 14: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

HTTP Streaming vs WebSocket Messaging• HTTP Streaming / SSE -- suited for simple streaming needs

• metrics, feeds

• STOMP / WebSocket scales to more advanced needs• finance, collaboration, games

• SockJS has lots of built-in smarts• fallback to polling if network/proxy prevents streaming• the extra mile for extra range of browsers

14

Page 15: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

CORS

15

Page 16: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

CORS Overview• Browsers prohibit requests to a different domain

• e.g. evil.com → bank.com

• CORS is a W3C specification for cross-domain requests

• For “simple” methods (GET, HEAD, POST)• browser sets Origin header

• Otherwise “preflight” request to obtain permission• HTTP OPTIONS• Origin and Access-Control-Request-Method headers

16

Page 17: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

CORS Support in Spring?• Tomcat, Jetty, others provide CORS Filter implementations

• First-class support within Spring however still beneficial

• More fine-grained control (based on request mappings)

• Layer central + local configuration

17

Page 18: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Local Configuration for @Controller• New @CrossOrigin annotation

• Supported on individual @RequestMapping methods

• Inherited from the class level

• Easy to configure one method or entire controller class hierarchy

18

Page 19: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Local Configuration for Simple Handlers• Implement CorsConfigurationSource

• Two built-in handlers that make use this contract• ResourceHttpRequestHandler • SockJsHttpRequestHandler

public interface CorsConfigurationSource { CorsConfiguration getCorsConfiguration(HttpServletRequest req);}

19

Page 20: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Central Configuration• The MVC Java config and MVC namespace expose CORS configuration

• Centralized configuration alternative• URL patterns based

• Layer central + local configuration• e.g. define general policy (e.g. allow csrf-token header)• refine locally

20

Page 21: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

More on CORS Configuration• AbstractHandlerMapping drives overall CORS handling

• It’s an internal feature that’s always on• cross-domain not allowed by default (as before)

• DispatcherServlet allows preflight requests in for handling• no need to set dispatchOptionsRequest

21

Page 22: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Spring MVC CORS and Spring Security• DispatcherServlet now provides CORS handling

• Spring Security needs to open HTTP OPTIONS (for preflight requests)

• There is work planned to improve this• only allow preflight requests with Spring MVC

22

Page 23: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

CorsFilter • Filter-based approach to CORS configuration

• Comparable to Tomcat, Jetty filters

• CORS handling strictly before DispatcherServlet

• CorsFilter can/should be ordered before Spring Security Filter chain

23

Page 24: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

HTTP Caching

24

Page 25: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Goals• Update default settings in line with current browsers/proxies

• Improve options local to @MVC controllers

• Provide use-case oriented + advanced config

• Various other improvements• Cache-Control directives, eTag/LastModified support

25

Page 26: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

CacheControl• Convenient “Cache-Control” response header value builder

• Covers the common use cases• CacheControl.maxAge(...)# cache for...• CacheControl.noStore() # prevent cache• CacheControl.noCache() # conditional cache + eTag/lastModified

• And the advanced• CacheControl.maxAge(...).noTransform().cachePublic()

26

Page 27: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

WebContentGenerator• Central HTTP cache configuration class

• WebContentInterceptor, ResourceHttpRequestHandler

• Defaults settings revised• no more HTTP 1.0 headers (“Expires” and “Pragma”)• cacheSeconds shortcut relies CacheControl

• Accepts CacheControl builder for its configuration

• Plus existing cacheSeconds shortcut (becomes CacheControl)

27

Page 28: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

@MVC• ResponseEntity supports HTTP cache settings

ResponseEntity.ok().cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS)).eTag(version) // lastModified also available.body(book);

• Sets response headers + checks client conditional headers

• Sends 304 (“not modified”) if eTag matches

28

Page 29: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

@MVC Continued• Explicit eTag / lastModified checks also possible

@RequestMappingpublic Object handle(WebRequest request) { long lastModified = ... if (request.checkNotModified(lastModified)) { return null; } // ... }

• Possible to check both eTag + lastModified (per recommendation)• request.checkNotModified(eTag,lastModified)

29

Page 30: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Resource Handling• Extended HTTP cache control for serving static resources

• MVC Java config• ResourceHandlerRegistry accepts CacheControl

• MVC XML namespace• <mvc:cache-control> element within <mvc:resources>

• eTag added when VersionResourceResolver is configured

30

Page 31: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Additional Reading• HTTP Caching Tutorial by Mark Nottingham

• Reference documentation HTTP Caching Support

• Notes in Migration from earlier versions wiki page

31

Page 32: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Composable@RequestMapping

32

Page 33: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Custom @RequestMapping• For example:

@PostJson("/input")public Object handle() { return … ;}

• Given custom annotation:

@RequestMapping(method=GET, produces="application/json")public @interface PostJson { String value() default "";}

33

Page 34: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Spring Shortcut Annotations? • Proposal to provide built-in shortcut annotations

• The challenge is finding the right mix

• Feedback based on specific use cases would help greatly

• SPR-13442 and spring-composed project

34

Page 35: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Jackson

35

Page 36: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Selective Serialization with @JsonView• Supported on @RequestMapping methods (since 4.1)

@RequestMapping @JsonView(SummaryView.class)public ResponseEntity<Foo> handle() { // … }

• Now also with @RequestBody

@RequestMapping(method = POST)public void handle(@JsonView(SummaryView.class) @RequestBody Foo foo) { // ...}

36

Page 37: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Jackson Modules• Jackson2ObjectMapperBuilder auto-registers Jackson modules

• jdk7, joda, jsr310, jdk8

• Conditional on classpath detection of target dependencies

• Both MVC Java config and XML namespace use Jackson2ObjectMapperBuilder

37

Page 38: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

@MVC

38

Page 39: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Programming Model • HandlerMethod argument for @ExceptionHandler methods

• could be handy with @ControllerAdvice

• CharSequence as controller method return value (alternative to String)• groovy.lang.GString, StringBuilder, StringBuffer

• @InitBinder methods can register Formatter for specific field(s)• otherwise typically global in ConversionService

39

Page 40: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Async Return Value Types• Built-in support for

• org.springframework.util.concurrent.ListenableFuture (4.1)• java.util.concurrent.CompletionStage (4.2)

• Both adapted to DeferredResult

• Easy to adapt others• RxJava Observable, Reactor Stream, …

• Follow example of built-in types

40

Page 41: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

JavaScript Templating

41

Page 42: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Why?• Traditionally HTML served then enhanced with JavaScript

• Trend towards generating the UI with JavaScript

• Address challenges for single-page apps

• As well as mobile devices

42

Page 43: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

React JavaScript Library• Developed at Facebook and Instagram

• Virtual DOM with automatic DOM updates, fast, efficient

• Supports server-side rendering (outside of DOM)

• Fast adoption by major Internet players• Facebook/Instagram, Yahoo!, Netflix, Airbnb, etc.

43

Page 44: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Server-Side Rendering• For React adopters, server-side rendering has significant benefits

• Faster load time, pre-generated HTML for web apps

• Search-engine friendly

• Client-centric UI still possible (e.g. Phonegap)

44

Page 45: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Spring 4.2 and React• New view templating choice for JSR-223 scripting engines

• ScriptTemplateView , ViewResolver, and Configurer

• Configuration very similar to the alternatives (e.g. FreeMarker)

• Use with Nashorn JavaScript engine from JDK 8

45

Page 46: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Additional Talks

Isomorphic Templating With Spring Boot, Nashorn and React

by Sebastien Deleuze

Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js by Matt Raible

46

Page 47: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

HTTP Byte Ranges

47

Page 48: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Byte Ranges• HTTP 1.1 supports ability to download partial content

• RFC 7233

• Server advertises willingness via Accept-Ranges response header

• Client can then send a Range request header• e.g. Range: bytes=500-999

• Large media files, request any part / resume …

48

Page 49: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Byte Range Support in Spring 4.2• HttpRange to create and parse ranges

• Accept-Range and Range exposed in HttpHeaders

• ResourceHttpRequestHandler supports byte ranges• e.g. server video files to iOS devices

49

Page 50: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

$ curl -v --range 0-99 \ http://localhost:8080/assets/jquery-2.1.4.js

> GET /assets/jquery-2.1.4.js HTTP/1.1> Range: bytes=0-99...< HTTP/1.1 206 Partial Content< ...< Accept-Ranges: bytes< Content-Range: bytes 0-99/247597< Content-Length: 100< ...

50

Example

Page 51: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Client Side

51

Page 52: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

RestTemplate with OkHttp• RestTemplate + AsyncRestTemplate can be used with OkHttp

• lightweight HTTP library

• HTTP library support• JDK• Apache HttpComponents• Netty • OkHttp

52

Page 53: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Flexible URI Template Handling• UriTemplateHandler for use with RestTemplate

• Default implementation delegates to UriComponentsBuilder

• Can be configured to• insert prefix in all URI templates• switch from path to path segment encoding by default

• Potential to plug in 3rd party URI template library• e.g. full RFC 6570 syntax

53

Page 54: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

WebSocket Messaging

54

Page 55: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Simple Broker: Heartbeats@Configuration@EnableWebSocketMessageBrokerstatic class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

// …

@Overridepublic void configureMessageBroker(MessageBrokerRegistry registry) { registry.enableSimpleBroker("/topic").setTaskScheduler(scheduler());}

@Bean @Primarypublic TaskScheduler scheduler() { return new ThreadPoolTaskScheduler();}

}

55

Enable

Page 56: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Simple Broker: Selector Expression

56

• Add selector header on subscribe

client.subscribe("/topic/interval", function(message) { // ... }, {'selector' : 'headers.parity == \'even\''});

• SpEL expression evaluated against messages

• Only matching messages received

Page 57: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Multi-Server User Destinations

57

• Spring supported special STOMP destination

• /user/{username}/…

• User receives message if connected

• User can be connected to any application server (since 4.2)

Page 58: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Multi-Server User Destinations

58

Spring App

RabbitMQ

Browser

Sergi

/user/sergi/…

Page 59: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Connected User Information• SimpUserRegistry bean

• Expose information about connected users and subscriptions

• Supported in multi-server scenarios• via UserRegistryMessageHandler

• Broadcast local user registry to other servers (via broker relay)• every 10 seconds• remote registry info expires after 20 seconds

59

Page 60: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

STOMP Client• Supported over WebSocket + over TCP

• Plug in any Spring WebSocketClient implementation

• SockJsClient allows using STOMP over WebSocket/SockJS

• Support for heartbeats, receipts

60

Page 61: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Programming Model• Property placeholders for @MessageMapping methods

• @JsonView on @Payload arguments

• Destination template variables in @SendTo annotation

• Async return value types• org.springframework.util.concurrent.ListenableFuture • CompletableFuture/CompletionStage (JDK 8)

61

Page 62: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Programming Model• @ControllerAdvice + @MessageExceptionHandler methods

• HandlerMethod arguments for @MessageExceptionHandler

• Spring OXM Marshaller based MessageConverter

62

Page 63: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Related Talk

From Zero to Hero with Spring WebSocket

by Sergi Almar

63

Page 64: Spring MVC 4.2: New and Noteworthy

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under aCreative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 64

Thank You for Listening!

@rstoya05

Learn More. Stay Connected.

@springcentral Spring.io/video