RestController in Spring 4.0

We are curiously waiting for the release of Spring 4.0 (Update : Spring 4.0 Released). This new 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. 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.

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.


import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
public class UserDetails {
	private String userName;
	private String emailId;

	public String getUserName() {
		return userName;
	public void setUserName(String userName) {
		this.userName = userName;

	public String getEmailId() {
		return emailId;
	public void setEmailId(String emailId) {
		this.emailId = emailId;


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;

public class SpringRestControllerDemo {
	@Autowired UserDetails userDetails;
			method=RequestMethod.GET,produces={"application/xml", "application/json"})
	public UserDetails getUser() {
		UserDetails userDetails = new UserDetails();
		return userDetails;

	@RequestMapping(value="/springcontent.htm", method=RequestMethod.GET)
	public String getUserHtml() {
		//Test HTML view
		return "example";


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
	<context:component-scan base-package="" />

	<bean id="userDetails" class=""/>
	<mvc:annotation-driven content-negotiation-manager="contentManager"/>
	<bean id="contentManager"
                <property name="favorPathExtension" value="true"/>
                <property name="ignoreAcceptHeader" value="true" />
                <property name="defaultContentType" value="text/html" />
                <property name="useJaf" value="false"/>
                <property name="mediaTypes">
	                    <entry key="json" value="application/json" />
	                    <entry key="html" value="text/html" />
	                    <entry key="xml" value="application/xml" />
	<bean id="jspViewResolver"
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />


With this new feature, we can use this specialized controller instead using the generic @Controller for the REST services.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!