Wednesday, February 10, 2010

REST Support in Spring 3.0 Part 1


With the release of Spring 3.0 creating a REST-style Web Service has never been easier.


URI Templates

The @RequestMapping controller annotation now supports URI Templates. A URI Template is a URI-like string, containing one or more variable names. When you substitute values for these variables, the template becomes a URI. This proposed RFC for URI Templates defines how a URI is parameterized.

For Example the URI template http://www.bti360.com/services/benefits/{type} contains the variable type. If we assign the variable the value 401k the template becomes http://www.bti360.com/services/benefits/401k.
During the processing of a request, the URI can be compared to an expected URI Template in order to extract a collection of variables.

Assuming the servlet name is services an example @RequestMapping for this URI template is @RequestMapping("/benefits/{type}")

With this support you no longer need to use a URL Rewriter filter to create REST-style URIs for you resources.

Content Negotiation

Another new addition is the ContentNegotiationViewResolver. This is a view resolver that looks at either a request's file extension or Accept headers to determine which view should handle the request. For example the request http://www.bti360.com/services/benefits/401k.pdf or http://www.bti360.com/services/benefits/401k with Accept Headers application/json would be handled by the pdf and json views respectively. Spring 3.0 comes with views supporting text, html, pdf, excel, json, xml, rss and atom out of the box, and it is very easy to add your own view class for any other media type.

Look for my next post where the topic will be REST client support in Spring 3.0.

No comments: