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>