What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations...
Transcript of What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations...
![Page 1: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/1.jpg)
Craig Walls [email protected] Twitter: @habuma @springsocial http://github.com/habuma
What’s new in Spring?
![Page 2: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/2.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
What’s new?
The Spring Ecosystem Compatibility Concerns
Configuration Improvements REST
WebSocket/STOMP Spring 4.1!
![Page 3: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/3.jpg)
Craig Walls [email protected] Twitter: @habuma @springsocial http://github.com/habuma
WebSocket, STOMP (and some other stuff that’s new in Spring)
![Page 4: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/4.jpg)
What’s new in the greater Spring Ecosystem?
![Page 5: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/5.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
New website!
http://spring.io
![Page 6: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/6.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Spring IO
![Page 7: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/7.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Spring Boot
@RestController class App { @RequestMapping("/") String home() { "Hi!" }}
This is a **complete** Spring application
Auto-configuration Dependency “starters”
Groovy CLI Actuator
![Page 8: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/8.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Spring Security and Java configuration
@Configuration@EnableWebMvcSecuritypublic class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {}
@Configuration@EnableWebMvcSecuritypublic class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {! @Autowired public void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("habuma").password("password").roles("USER"); }!}
@Configuration@EnableWebMvcSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter{! @Autowired private DataSource dataSource; @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/secret/**").authenticated() .antMatchers("/**").permitAll() .and() .formLogin() .loginPage("/login"); } ! @Autowired public void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception { auth .jdbcAuthentication() .dataSource(dataSource) .usersByUsernameQuery("select username, password, enabled from myapp_users where username=?") .authoritiesByUsernameQuery("select username, authority from myapp_auths where username=?"); } }
![Page 9: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/9.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Spring Data REST & ALPS
Spring Data produces repository implementations
Spring Data REST produces REST APIs (with HAL linking)
Spring Data REST will provide ALPS metadata
![Page 10: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/10.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
JSON Patch and Differential Sync
How to handle PATCH requests in Spring MVC?
JSON Patch to describe resource diffs
Differential Synchronization for live updates
![Page 11: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/11.jpg)
Compatibility
![Page 12: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/12.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Java 8 support
Lambdas and method references for Spring callback interfaces
java.time support
Parameter name discovery
Annotations retrofitted with @Repeatable
Compatible with Java 6 and 7
![Page 13: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/13.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Java 8 lambda support
public List<Book> findAllBooks() { return jdbc.query( "select isbn, title, author from books", new RowMapper<Book>() { public Book mapRow(ResultSet rs, int rowNum) throws java.sql.SQLException { return new Book( rs.getString("isbn"), rs.getString("title"), rs.getString("author")); }; });}
public List<Book> findAllBooks() { return jdbc.query( "select isbn, title, author from books", (rs, rowNum) -> { return new Book( rs.getString("isbn"), rs.getString("title"), rs.getString("author")); });}
![Page 14: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/14.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
JavaEE 6 and 7
Java EE 6 is now the baseline
JPA 2.0 / Servlet 3.0
Servlet 2.5 supported (for the sake of GAE)
Servlet 3.0+ recommended
![Page 15: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/15.jpg)
Configuration
![Page 16: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/16.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Conditional configuration@Configuration@Conditional(ThymeleafCondition.class)public class ThymeleafConfig { @Bean public SpringTemplateEngine templateEngine(TemplateResolver templateResolver) { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver); return templateEngine; }! @Bean public ThymeleafViewResolver viewResolver( SpringTemplateEngine templateEngine) { ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); viewResolver.setTemplateEngine(templateEngine); return viewResolver; }! public TemplateResolver templateResolver() { TemplateResolver templateResolver = new ServletContextTemplateResolver(); templateResolver.setPrefix("/WEB-INF/views/"); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode("HTML5"); return templateResolver; }}
public class ThymeleafCondition implements Condition { public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { try { context.getClassLoader().loadClass( "org.thymeleaf.spring4.SpringTemplateEngine"); return true; } catch (ClassNotFoundException e) { return false; } }}
![Page 17: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/17.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Generics as autowire qualifiers
@Autowiredpublic ThingKeeper(Thing<String> stringThing, Thing<Integer> intThing) { this.stringThing = stringThing; this.intThing = intThing;}
@Configuration@ComponentScanpublic class GenericQualifierConfig {! @Bean public Thing<String> stringThing() { return new Thing<String>("Hello"); } @Bean public Thing<Integer> intThing() { return new Thing<Integer>(42); }!}
![Page 18: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/18.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Ordered list injection
@Componentpublic class Shooters {! private List<Shooter> shooters;! @Autowired public Shooters(List<Shooter> shooters) { this.shooters = shooters; }! public List<Shooter> getShooters() { return shooters; }!}
@Configuration@ComponentScanpublic class ShooterConfig {! @Bean @Order(1) public Shooter greedo() { return new Shooter("Greedo"); } @Bean @Order(0) public Shooter han() { return new Shooter("Han Solo"); } }
![Page 19: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/19.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Meta-annotations and attributes
@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.METHOD, ElementType.TYPE})@Service@Cacheable(value="myCache", key="#id")@Transactional(propagation=Propagation.REQUIRES_NEW, isolation=Isolation.REPEATABLE_READ)public @interface SlowTransactionalService {}
@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.METHOD, ElementType.TYPE})@Service@Cacheable(value="myCache", key="#id")@Transactional(propagation=Propagation.REQUIRES_NEW, isolation=Isolation.REPEATABLE_READ)public @interface SlowTransactionalService { Propagation propagation(); Isolation isolation(); String key();!}
![Page 20: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/20.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Groovy configuration
GenericGroovyApplicationContext appContext = new GenericGroovyApplicationContext("com/habuma/spring4fun/beans.groovy");
import com.habuma.spring4fun.Foo;import com.habuma.spring4fun.Bar;!beans {! bar(Bar) { name = "Cheers" }! foo(Foo) { bar = bar }!}
![Page 21: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/21.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Also…
@Component@Description("The Thing bean")public class Thing { … }
@Bean@Description("The production data source")public DataSource dataSource() { … }
@Description
@Autowired@Lazypublic void setDataSource(DataSource ds) { … }
@Lazy at injection point
CGLib proxies no longer require a default constructor
![Page 22: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/22.jpg)
REST
![Page 23: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/23.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Simpler REST controllers
@Controller@RequestMapping("/books")public class BookController {! @RequestMapping(method=RequestMethod.GET) @ResponseBody public List<Book> allBooks() { ... }! @RequestMapping(value="/{isbn}", method=RequestMethod.GET) @ResponseBody public Book bookByIsbn(@PathVariable String isbn) { ... }! @RequestMapping(value="/search", method=RequestMethod.GET) @ResponseBody public List<Book> search(@RequestParam("q") String query) { ... } }
@RestController@RequestMapping("/books")public class BookController {! @RequestMapping(method=RequestMethod.GET) public List<Book> allBooks() { ... }! @RequestMapping(value="/{isbn}", method=RequestMethod.GET) public Book bookByIsbn(@PathVariable String isbn) { ... }! @RequestMapping(value="/search", method=RequestMethod.GET) public List<Book> search(@RequestParam("q") String query) { ... } }
![Page 24: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/24.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Asynchronous REST clients
RestTemplate rest = new RestTemplate();ResponseEntity<Profile> response = rest.getForEntity("http://graph.facebook.com/4", Profile.class);
AsyncRestTemplate rest = new AsyncRestTemplate();!ListenableFuture<ResponseEntity<Profile>> future = rest.getForEntity("http://graph.facebook.com/4", Profile.class);!future.addCallback(new ListenableFutureCallback<ResponseEntity<Profile>>() { public void onSuccess(ResponseEntity<Profile> response) { ... }; public void onFailure(Throwable throwable) { ... }});
![Page 25: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/25.jpg)
WebSocket/STOMP
![Page 26: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/26.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
What is WebSocket?
Full duplex communication channel !
Enables the server to talk to the browser*
* What you really want to do
![Page 27: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/27.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
How Spring supports WebSocket
Low-level API SockJS support
Higher-level Spring MVC-based API Messaging template
![Page 28: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/28.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
The problem with WebSocket
It’s not ubiquitous Browser support iffy Server support iffy
Proxy server support iffy
![Page 29: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/29.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Introducing SockJS
WebSocket emulator Mirrors WebSocket API closely
Tries WebSocket first Falls back to…
XHR Streaming, XDR Streaming, IFrame Event Source, IFrame HTML File, XHR Polling, XDR Polling, IFrame XHR Polling, JSONP Polling
![Page 30: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/30.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
More problems with WebSocket
Too low-level No messaging semantics
![Page 31: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/31.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Introducing STOMP
SENDdestination:/app/marcocontent-length:20!{\"message\":\"Marco!\"}
Simple Text Oriented Messaging Protocol Offers HTTP-like message semantics
Emphasis on messaging More akin to JMS and AMQP
Subscribe/send, not request/response
![Page 32: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/32.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Enabling STOMP
@Configuration@EnableWebSocketMessageBrokerpublic class WebSocketStompConfig extends AbstractWebSocketMessageBrokerConfigurer {! @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/marcopolo").withSockJS(); }! @Override public void configureMessageBroker(MessageBrokerRegistry registry) { registry.enableSimpleBroker("/queue/", "/topic/"); registry.setApplicationDestinationPrefixes("/app"); } }
![Page 33: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/33.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Simple broker message flow
![Page 34: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/34.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Enabling STOMP over a broker relay
@Configuration@EnableWebSocketMessageBrokerpublic class WebSocketStompConfig extends AbstractWebSocketMessageBrokerConfigurer {! @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/marcopolo").withSockJS(); }! @Override public void configureMessageBroker(MessageBrokerRegistry registry) { registry.enableStompBrokerRelay("/queue/", "/topic/"); registry.setApplicationDestinationPrefixes("/app"); } }
![Page 35: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/35.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Broker relay message flow
![Page 36: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/36.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Handling messages
@Controllerpublic class MarcoController {! private static final Logger logger = LoggerFactory .getLogger(MarcoController.class);! @MessageMapping("/marco") public Shout handleShout(Shout incoming) { logger.info("Received message: " + incoming.getMessage());! try { Thread.sleep(2000); } catch (InterruptedException e) {} Shout outgoing = new Shout(); outgoing.setMessage("Polo!"); return outgoing; }!}
![Page 37: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/37.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Handling subscriptions
@Controllerpublic class GamedayScoresController {! @SubscribeMapping({"/scores"}) public List<Score> scores() { … }!}
![Page 38: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/38.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Handling exceptions
@Controllerpublic class MarcoController {! private static final Logger logger = LoggerFactory .getLogger(MarcoController.class);! @MessageExceptionHandler(SomeException.class) @SendTo("/topic/marco") public Shout handleException(Throwable t) { Shout s = new Shout(); s.setMessage("EXCEPTION: " + t.getMessage()); return s; }!}
![Page 39: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/39.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Sending messages
@Componentpublic class RandomNumberMessageSender {! private SimpMessagingTemplate messaging;! @Autowired public RandomNumberMessageSender(SimpMessagingTemplate messaging) { this.messaging = messaging; } @Scheduled(fixedRate=10000) public void sendRandomNumber() { Shout random = new Shout(); random.setMessage("Random # : " + (Math.random() * 100)); messaging.convertAndSend("/topic/marco", random); } }
![Page 40: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/40.jpg)
Freshly Picked: Spring 4.1
![Page 41: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/41.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Spring MVC ImprovementsStatic resource resolution/transformation Support for JSONP in controller methods
Jackson @JsonView support ResponseBodyAdvice
java.util.Optional support for method parameters DeferredResult can be returned
GSon and Google Protocol Buffers message converters
Improved web testing support
![Page 42: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/42.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Jackson @JsonView
@JsonView(Views.Public.class)@RequestMapping(method=GET, value=“/users/{id}“)public @ResponseBody UserProfile profile(@PathVariable(“id”) long id) { return userRepo.findOne(id);} public class UserProfile {
@JsonView(Views.Public.class) String name; @JsonView(Views.Privileged.class) String email; @JsonView(Views.Internal.class) String ssn;! …}
public class Views { public static class Public {} public static class Privileged {} public static class Internal {}}
![Page 43: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/43.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
JSONP
@ControllerAdviceprivate class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {! public JsonpAdvice() { super(“callback”); }!}
![Page 44: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/44.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Optional parameters
@RequestMapping(method=GET, value="/books" )public @ResponseBody List<Book> allBooks(Optional<Long> maxResults) {! Long max = maxResults.isPresent() ? maxResults.get() : Long.MAX_VALUE);! …}
![Page 45: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/45.jpg)
Email: [email protected] Twitter: @habuma Blog: http://www.springinaction.com Sample Code: http://github.com/habuma
Caching Improvements
CacheResolver abstraction !
Operation-level customizations !
Class-level customizations !
JCache integration
@Cachablepublic Book findBook(ISBN isbn) { … }
@Cachable(value=“book”, keyGenerator=“myKeyGenerator”)public Book findBook(ISBN isbn) { … }
@CacheConfig(cacheName=“book”, keyGenerator=“myKeyGenerator”)public class BookRepository { … }
![Page 46: What’s new in Spring? · Spring Data REST & ALPS Spring Data produces repository implementations Spring Data REST produces REST APIs ... return jdbc.query( "select isbn, title,](https://reader033.fdocuments.in/reader033/viewer/2022053100/6057c0868491c84b0d256fc9/html5/thumbnails/46.jpg)
Thank you!