How to write RESTful web services using spring mvc

Spring is a well-known framework for building Java™ Platform, Enterprise Edition (Java EE) applications, now it supports Representational State Transfer (REST) in its Model-View-Controller (MVC) layer. It’s important for RESTful web services to produce multiple representations based on the client requests. In this article, learn how to use Spring APIs and annotations to build RESTful web services that produce popular representations such as ATOM Feed, XML, and JavaScript Object Notation (JSON).

What is REST?
REST is an architectural style that has evolved from existing web technologies to allow client applications to communicate with a server using a simple and familiar approach. The approach is familiar because REST is built on top of the HTTP protocol, a protocol that has formed the backbone of the web for years. REST leverages the existing capabilities of HTTP and uses them to provide an architectural approach for implementing a simple and effective client server model.

RESTFul webservices: A RESTFul webservices are based on the HTTP methods and the concept of REST. A RESTFul webservice typically defines the base URI for the services, the supported MIME-types (XML, Text, JSON, user-defined,..) and the set of operations (POST, GET, PUT, DELETE) which are supported.

REST support in Spring3MVC:@Controller:-
Use the @Controller annotation to annotate the class that will be the controller in MVC and handle the HTTP request.

@RequestMapping:-
Use the @RequestMapping annotation to annotate the function that should handle certain HTTP methods, URIs, or HTTP headers. This annotation is the key to the Spring REST support. You change the method parameter to handle other HTTP methods.
For example:@RequestMapping(method=RequestMethod.GET, value=”/emps”,
headers=”Accept=application/xml, application/json”)

@PathVariable:-
A path variable in the URI could be injected as a parameter using the @PathVariable annotation.
For example:@RequestMapping(method=RequestMethod.GET, value=”/emp/{id}”)
public ModelAndView getEmployee(@PathVariable String id) { … }

Other useful annotations
Use @RequestParam to inject a URL parameter into the method.
Use @RequestHeader to inject a certain HTTP header into the method.
Use @RequestBody to inject an HTTP request body into the method.
Use @ResponseBody to return the content or object as the HTTP response body.
Use HttpEntity to inject into the method automatically if you provide it as a parameter.
Use ResponseEntity to return the HTTP response with your custom status or headers.For example:public @ResponseBody Employee getEmployeeById(@RequestParam(“name”)
String name, @RequestHeader(“Accept”) String accept, @RequestBody String body) {…}
public ResponseEntity method(HttpEntity entity) {…}

For writing the web application which support RESTfull url then we have follow the steps.Step 1: Adding the following another jars to the Libs folder.
jaxb-api-2.1.jar
jaxen-1.1.1.jar

Step 3: Add model classes: There are two model classes Employee.java and Employees.java
These classes will be having JAXB annotations, which will be used by marshaller to convert them in appropriate xml or json formats.Employees.java

Step 4: Write the controller – WebServiceController.java
Our WebServiceController.java will created to have REST specific annotations for path mappings in request parameters mappings. Also, we will specify the header attributes for request and response.