REST Services in Spring
by, 11-28-2011 at 01:28 AM (1423 Views)
In Spring 3.0, the Spring team added support for Representational State Transfer (REST). It has emerged as a popular information-centric alternative to traditional SOAP-based web services. Spring developers have taken advantage of the REST architectural model to pack the Spring MVC with first-class support for working with REST. In this tip, we will look at the fundamentals of REST.
In order to understand what REST is all about, it helps to break down the acronym into its constituent parts:
- State—When working with REST, we’re more concerned with the state of a resource than with the actions we can take against resources.
- Transfer—REST involves transferring resource data, in some representational form, from one application to another.
Basically REST is about transferring the state of resources in the most appropriate form from a server to a client (or vice versa). I will emphasize the resource-oriented nature of REST and speak of RESTful resources.
Spring REST Support is based on the following:
- RestTemplate can be used to simplify client-side consumption of REST resources.
- Controllers can handle requests for all HTTP methods, including the four primary REST methods: GET, PUT, DELETE, and POST.
- The <form:form> JSP tag from Spring’s form-binding JSP tag library and the new HiddenHttpMethodFilter, makes it possible to submit PUT and DELETE requests from HTML forms, even in browsers that don’t support these HTTP methods.
- Resources can be represented in a variety of ways using Spring’s view and view resolvers, including new view implementations for rendering model data as XML, JSON, Atom, and RSS.
- The best representation suited for a client can be chosen using the ContentNegotiatingViewResolver.
- View-based rendering can be bypassed altogether using the new @ResponseBody annotation and various HttpMethodConverter implementations.
- The new @RequestBody annotation and the HttpMethodConverter implementations can convert inbound HTTP data into Java objects passed into a controller’s handler methods.
- The @PathVariable annotation enables controllers to handle requests for parameterized URLs (URLs that have variable input as part of their path).
In the next tip, we will look at writing RESTful Controllers.