Spring 4.0 重要的一个新的改进是增长了 @RestController 注解,它继承自 @Controller 注解。4.0以前的版本,Spring MVC 的组件都使用 @Controller 来标识当前类是一个控制器servlet。html
使用这个特性,咱们能够在开发REST服务的时候能够不使用 @Controller+@ResponseBody 的方式而只使用 @RestController。java
当你使用实现一个 RESTful web services 的时候,response 将一直经过 response body 发送。为了简化开发,Spring 4.0提供了一个专门版本的controller:@RestController。web
下面咱们来看看@RestController实现的定义:spring
@Target(value=TYPE) @Retention(value=RUNTIME) @Documented @Controller @ResponseBody public @interface RestController
官方文档解释:json
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. spring-mvc注解自己使用@Controller和@ResponseBody注解。
使用了这个注解的类会被看做一个controller-使用
@RequestMapping的方法有一个默认的@ResponseBody注解。 mvc@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. app@ResponseBody也能够加到类一级,经过继承方法一级不须要添加。jsp
SpringRestControllerDemo.javathis
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="com.infosys.rest" /> <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>