Sunday, August 25, 2013

Restful web services using Spring 3 MVC Part 1



According to wikipedia :
REST-style architectures conventionally consist of clients and servers. Clients initiate requests to servers; servers process requests and return appropriate responses. Requests and responses are built around the transfer of representations of resources. A resource can be essentially any coherent and meaningful concept that may be addressed. A representation of a resource is typically a document that captures the current or intended state of a resource.
The client begins sending requests when it is ready to make the transition to a new state. While one or more requests are outstanding, the client is considered to be in transition. The representation of each application state contains links that may be used the next time the client chooses to initiate a new state-transition.
In addition to URIs; Internet media types; request and response codes; etc., HTTP has a vocabulary of operations called request methods, most notably:
GET
POST
PUT
DELETE
REST uses these operations and other existing features of the HTTP protocol. For example, layered proxy and gateway components perform additional functions on the network, such as HTTP caching and security enforcement.
In this post I am discussing about GET request method.
CODING
Let’s start with a simple Spring MVC application created with Spring MVC template. In servlet-context.xml where component-scan, annotation-driven and InternalResourceViewResolver are registered.
Now create two simple POJO classes Employee and EmployeeList. EmployeeList contains one attribute List with getter and setters. Classes are converted to its XML representation using Jaxb annotation @XmlRootElement. Jaxb allows developers to map Java classes to XML representations and vice-versa.
Employee.java EmployeeList.java
Now create one class RestProviderController.java which is annoted as @Controller. This class will work as controller and call to it's respective method according to URL mapping. getEmployees() return EmployeeList an-noted as @ResponseBody. @RequestMapping used an another parameter header attributes for request and response.
RestProviderController.java
Create a war of project named employeeMgt.war and deployed on tomcat. I am running my tomcat at port 9090. Hit the URL from any rest client(I am using RestClient A firefox plugin for testing the RESTful webservices).
URL : http://localhost:9090/employeeMgt/restService/employees/
If I am returning a EmployeeList object and output is a XML, where is conversion between object and XML? A new concept: HttpMessageConverters. HttpMessageConverter is responsible for converting from HTTP request message to an object and converting from an object to HTTP response body. Next HttpMessageConverters are registered by default:
Now instead of returning XML we want to return JSON. Change could not be easier, add Jackson library in lib and change @XmlRootElement to @JsonAutoDetect. And now MappingJacksonHttpMessageConverter will handle this object and will transform Character instance to JSON protocol using Jackson library. Only changing one line of code!!!

No comments:

Post a Comment