Srivatsan Sundararajan
Senior Manager, QA for MyEclipse, DevStyle and CodeTogether.
Contributions by Swapna Sagi
Senior Manager, QA for MyEclipse, DevStyle and CodeTogether.
Contributions by Swapna Sagi
Spring’s annotation based MVC framework simplifies the process of creating RESTful web services. The key difference between a traditional Spring MVC controller and the RESTful web service controller is the way the HTTP response body is created. While the traditional MVC controller relies on the View technology, the RESTful web service controller simply returns the object and the object data is written directly to the HTTP response as JSON/XML. For a detailed description of creating RESTful web services using the Spring framework, click here.
Figure 1: Spring MVC traditional workflow
The following steps describe a typical Spring MVC REST workflow:
In Figure 1, notice that in the traditional workflow the ModelAndView object is forwarded from the controller to the client. Spring lets you return data directly from the controller, without looking for a view, using the @ResponseBody annotation on a method. Beginning with Version 4.0, this process is simplified even further with the introduction of the @RestController annotation. Each approach is explained below.
When you use the @ResponseBody annotation on a method, Spring converts the return value and writes it to the http response automatically. Each method in the Controller class must be annotated with @ResponseBody.
Figure 2: Spring 3.x MVC RESTful web services workflow
Spring has a list of HttpMessageConverters registered in the background. The responsibility of the HTTPMessageConverter is to convert the request body to a specific class and back to the response body again, depending on a predefined mime type. Every time an issued request hits @ResponseBody, Spring loops through all registered HTTPMessageConverters seeking the first that fits the given mime type and class, and then uses it for the actual conversion.
Let’s walk through @ResponseBody with a simple example.
package com.example.spring.model; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "Employee") public class Employee { String name; String email; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Employee() { } }
package com.example.spring.rest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.example.spring.model.Employee; @Controller @RequestMapping("employees") public class EmployeeController { Employee employee = new Employee(); @RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = "application/json") public @ResponseBody Employee getEmployeeInJSON(@PathVariable String name) { employee.setName(name); employee.setEmail("employee1@genuitec.com"); return employee; } @RequestMapping(value = "/{name}.xml", method = RequestMethod.GET, produces = "application/xml") public @ResponseBody Employee getEmployeeInXML(@PathVariable String name) { employee.setName(name); employee.setEmail("employee1@genuitec.com"); return employee; } }
Notice the @ResponseBody added to each of the @RequestMapping methods in the return value.
Spring 4.0 introduced @RestController, a specialized version of the controller which is a convenience annotation that does nothing more than add the @Controller and @ResponseBody annotations. By annotating the controller class with @RestController annotation, you no longer need to add @ResponseBody to all the request mapping methods. The @ResponseBody annotation is active by default. Click here to learn more.
Figure 5: Spring 4.x MVC RESTful Web Services Workflow
To use @RestController in our example, all we need to do is modify the @Controller to @RestController and remove the @ResponseBody from each method. The resultant class should look like the following:
package com.example.spring.rest; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.example.spring.model.Employee; @RestController @RequestMapping("employees") public class EmployeeController { Employee employee = new Employee(); @RequestMapping(value = "/{name}", method = RequestMethod.GET, produces = "application/json") public Employee getEmployeeInJSON(@PathVariable String name) { employee.setName(name); employee.setEmail("employee1@genuitec.com"); return employee; } @RequestMapping(value = "/{name}.xml", method = RequestMethod.GET, produces = "application/xml") public Employee getEmployeeInXML(@PathVariable String name) { employee.setName(name); employee.setEmail("employee1@genuitec.com"); return employee; } }
Note that we no longer need to add the @ResponseBody to the request mapping methods. After making the changes, running the application on the server again results in same output as before.
As you can see, using @RestController is quite simple and is the preferred method for creating MVC RESTful web services starting from Spring v4.0. I would like to extend a big thank you to my co-author, Swapna Sagi, for all of her help in bringing you this information!