Get ahead
VMware offers training and certification to turbo-charge your progress.
Learn moreRecently 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:
than Jackson; has already been in use in Spring Android.
(just missed RC1 but already in master for 4.1 RC2) -- efficient and effective as an inter-service communication data protocol within an enterprise but can also be exposed as JSON and XML for browsers. This comes through a contribution from Alex Antonov.
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:
content negotiation you'll likely appreciate this one. See the updated documentation.
without the need for controller logic, view controllers now have built-in support for redirecting and setting the response status. An application can use this to configure redirect URLs, render 404 responses with a view, send "no content" responses, etc. Some use cases listed here.
and have also been backported to 4.0. See the updated documentation.
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:
JSON Assert as an extra option to using JSONPath much like it has been possible to do for XML with XMLUnit.
MockMvcConfigurer
.This was added to make it easy to apply Spring Security setup but can be used to encapsulate common setup for any 3rd party framework or within a project.
MockRestServiceServer
now supports the AsyncRestTemplate
for client-side testing.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.