RESTful Web Services in Spring 3

We have discussed what REST & RESTful Web Services are. The RESTful Web Services are built on top of the simple HTTP request and response protocol and use the REST style for the representation state transfer. We will now explore how to implement such a service using Spring 3.0.

 

There are lots of technologies out there that can be used to implement RESTful Web Services. However, I found that Spring 3 provides a simple and easy implementation.

 

Spring is famous in its MVC (Model-View-Controller) framework. With MVC, the view is decoupled from the model. That means, you can easily provide different views or representations of the model, e.g. HTML representation, XML representation, and JSON representation.

 

Spring 3.0 offers many features, but the following two features should be enough to implement RESTful Web Services.

 

URI Templates

You can map a REST request to a specific method. The mapping also supports parameter handling. You can specify a path section as a variable and pass it into your method as a parameter.

@RequestMapping(method=RequestMethod.GET, value="/customer/{id}")
public ModelAndView showCustomer(@PathVariable("id") Integer id) {
Customer customer = customers.get(id);
ModelAndView mv = new ModelAndView(CUSTOMER_VIEW_KEY);
mv.addObject("customer", customer);
return mv;
}

When a request comes in for /customer/123, that 123 will be bound to the id parameter.

 

Content Negotiation

Spring provides a generic content resolver to handle different content requests, by adding an extension or specifying in the Accept header to indicate the expected format, e.g. http://localhost:8080/app/customer/123.xml retrieves the XML representation of the customer 123.

 

Here is an example of using the ContentNegotiatingViewResolver to handle JSON & XML requests and the conversion.

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
<entry key="xml" value="application/xml"/>
</map>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"/>
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="autodetectAnnotations" value="true"/>
</bean>
</constructor-arg>
</bean>
</list>
</property>
</bean>

 

Together with the default HTML view resolver, as shown in the configuration below, it can now accept requests, like /customers.html, /customer.xml, or /customer.json, and returns the appropriate format, such as HTML, XML, or JSON respectively.

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

 

I have built a very simple web application to demonstrate this idea. It handles two kinds of requests: customer list and individual customer details.

 

If you send a request like http://localhost:8080/restful/app/customers.html, http://localhost:8080/restful/app/customers.xml, or http://localhost:8080/restful/app/customers.json, it returns the customer list in an appropriate format, i.e. HTML, XML, or JSON. If you are interested in a particular customer, you make a request like http://localhost:8080/restful/restful/app/customer/123.html, http://localhost:8080/restful/app/customer/123.xml, or http://localhost:8080/restful/app/customer/123.json. It will return the customer details in either HTML, XML, or JSON.

 

It is very easy to develop your RESTful solution using the Spring MVC 3.0. If you are interested in my sample project, you can download the restful.war file and deploy it to any web container, e.g. tomcat. I have tested it with tomcat 6.0.18.