β

Spring Framework 4.1 -- Spring MVC Improvements

Spring 943 阅读

Recently Juergen Hoeller announced the availability of the first of two 4.1 release candidates. Brian Clozel followed up with a post on the static web resources handling enhancements. Previously Stephane Nicoll blogged about cache and JMS related improvements. The goal of this post is to summarize Spring MVC improvements.

The JDK 1.8 java.util.Optional is now supported for @RequestParam, @RequestHeader and @MatrixVariable controller method arguments while ListenableFuture is supported as a return value alternative to DeferredResult where an underlying service (or perhaps a call to AsyncRestTemplate) already returns ListenableFuture.

Jackson's @JsonView is supported directly on @ResponseBody and ResponseEntity controller methods for serializing different amounts of detail for the same POJO, e.g. summary vs detail page. This is also supported with View-based rendering by adding the serialization view type as a model attribute under a special key.

JSONP is now supported with Jackson. For response body methods declare an @ControllerAdvice as shown below. For View-based rendering simply configure the JSONP query parameter name(s) on MappingJackson2JsonView.

@ControllerAdvice
private static class JsonpAdvice extends AbstractJsonpResponseBodyAdvice {

    public JsonpAdvice() {
        super("callback");
    }

}

Note that @ControllerAdvice was introduced in 3.2 for @ExceptionHandler, @ModelAttribute, and @InitBinder methods shared across all or a subset of controllers. ResponseEntityExceptionHandler is one example for global exception handling by writing error details to the body of the response. In 4.1 an @ControllerAdvice can also implement ResponseBodyAdvice in which case it will be called after the controller method returns but before the response is written and therefore committed. This has a number of useful applications with @JsonView the JSONP already serving as two examples built on it.

Two new HttpMessageConverter types:

MvcUriComponentsBuilder was introduced in 4.0 as a way of building links to controller methods through controller method invocation (similar to mock testing). In 4.1 views such as JSPs can also build links to controllers by referring to their mappings by name. A default name is assigned to every @RequestMapping. For example FooController with method handleFoo is assigned "FC#handleFoo" by default but the naming strategy is customizable and can be also be set explicitly through the new name attribute on @RequestMapping. A new mvcUrl Spring JSP tag makes this easy to use in JSP pages. The same can be done for any other view technology.

The familiar ResponseEntity now has a builder-style API that guides controller methods towards the preparation of server-side responses, e.g. ResponseEntity.ok(). For the client side there is a new RequestEntity also offering a builder-style API that guides towards the preparation of client-side HTTP requests.

MVC Java config and XML namespace:

Speaking of view resolution in 4.1 we've added support for view rendering through Groovy markup templates available in Groovy 2.3. If you've been waiting for DRY markup, along the lines of HAML (Ruby on Rails), this one is for you.

Spring MVC Test related:

The Spring MVC Test HtmlUnit extension (separate project) has also been progressing steadily with an M1 milestone. If this is of interest do check out the project's home page.

Spring Frawork 4.1 is just around the corner (end of August) and so is SpringOne 2GX 2014 in early September in Dallas, TX. Take a close look at these improvements to see if they meet your needs or if they need further improvement. Do come to this year's SpringOne where we have a great lineup for the Web and JavaScript track.

作者:Spring
原文地址:Spring Framework 4.1 -- Spring MVC Improvements, 感谢原作者分享。

发表评论