使用這個特性,我們可以開發rest服務的時候不需要使用@controller而專門的@restcontroller。
當你實作一個restful web services的時候,response将一直通過response body發送。為了簡化開發,spring 4.0提供了一個專門版本的controller。下面我們來看看@restcontroller實作的定義:

@target(value=type)
@retention(value=runtime)
@documented
@controller
@responsebody
public @interface restcontroller
官方文檔解釋:

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.
注解本身使用@controller和@responsebody注解。使用了這個注解的類會被看作一個controller-使用@requestmapping的方法有一個預設的@responsebody注解。
@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.
@responsebody也可以加到類一級,通過繼承方法一級不需要添加。
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;
public string getemailid() {
return emailid;
public void setemailid(string emailid) {
this.emailid = emailid;
}
springrestcontrollerdemo.java

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("[email protected]");
return userdetails;
@requestmapping(value="/springcontent.htm", method=requestmethod.get)
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="com.infosys.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>