In this tutorial I am going to explain one of the new spring annotation @RestController introduced as part Spring 4 release. The spring 4 version comes up with lot of exciting new features. One of the API improvements is new RestController annotation which is inherited from the @Controller annotation.
If you are interested in receiving more updates related to spring framework, spring boot, etc. please subscribe here.
RestController in Spring 4
Prior to the version 4.0, all the Spring MVC components has to use the common @Controller annotation to mark that as the controller servlet. When you implement a RESTful web services, the response would be always sent with the response body. To make this simple, Spring 4.0 has provided a specialized version of controller. Look at the definition of the @RestController implementation.
@Target(value=TYPE) @Retention(value=RUNTIME) @Documented @Controller @ResponseBody public @interface RestController
Spring docs says:
A convenience annotation that is itself annotated with @Controller and @ResponseBody. Types that carry this annotation are treated as controllers where @RequestMapping methods assume @ResponseBody semantics by default.
@ResponseBody – As of version 4.0 this annotation can also be added on the type level in which case is inherited and does not need to be added on the method level.
This tutorial explains with a simple example on how to use the annotation @RestController. I am using the same example of Content Negotiation in Spring MVC 3.2. If you compare both the examples, you would notice the difference.
RestController Example
Here is the example code for RestController annotation.
- Also Read : REST API Best Practices
UserDetails.java
package javabeat.net.rest; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class UserDetails { private String userName; private String emailId; @XmlAttribute public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @XmlAttribute public String getEmailId() { return emailId; } public void setEmailId(String emailId) { this.emailId = emailId; } }
SpringRestControllerDemo.java
package javabeat.net.rest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; @RestController public class SpringRestControllerDemo { @Autowired UserDetails userDetails; @RequestMapping(value="/springcontent", method=RequestMethod.GET,produces={"application/xml", "application/json"}) @ResponseStatus(HttpStatus.OK) public UserDetails getUser() { UserDetails userDetails = new UserDetails(); userDetails.setUserName("Krishna"); userDetails.setEmailId("krishna@gmail.com"); return userDetails; } @RequestMapping(value="/springcontent.htm", method=RequestMethod.GET) @ResponseStatus(HttpStatus.OK) public String getUserHtml() { //Test HTML view return "example"; } }
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="javabeat.net.rest" /> <bean id="userDetails" class="javabeat.net.rest.UserDetails"/> <mvc:annotation-driven content-negotiation-manager="contentManager"/> <bean id="contentManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> <property name="favorPathExtension" value="true"/> <property name="ignoreAcceptHeader" value="true" /> <property name="defaultContentType" value="text/html" /> <property name="useJaf" value="false"/> <property name="mediaTypes"> <map> <entry key="json" value="application/json" /> <entry key="html" value="text/html" /> <entry key="xml" value="application/xml" /> </map> </property> </bean> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
With this new feature, we can use this specialized controller instead using the generic @Controller for the REST services.
Summary
In this tutorial I have explained the use of RestController annotation in spring mvc application. I have used the XML configurations for writing this example.
If you have any questions on using RestController, please drop your comments.