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.



About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

  • Steve Johann

    Thanks for the tutorials!

  • kiran

    Hi Krishna, It is very nice artical for people can make them understand easly.

    Kiran B

  • Sadique Khan

    Hi Krishna,

    I am trying to implement this example in Eclipse. I am unable to get any response with it. The SpringRestControllerDemo just doesnt seem to get picked. If I place another contoller class with @Controller, it works fine. Please help me.

    • Krishna Srinivasan

      Are you getting any error?

      • Tukuna

        I am trying this example and getting below error. I tried with debugging mode observer I am getting below error in the return statement. I am trying to return UserDetails object

        Error 406: SRVE0295E: Error reported: 406

  • Martin

    Thanks, but I don’t get the meaning of the userDetails bean.