分享

Spring4.0系列3-@RestController

 贾朋亮博客 2015-07-10
 

Spring4.0系列1-新特性

Spring4.0系列2-环境搭建

Spring4.0系列3-@RestController

Spring4.0系列4-Meta Annotation(元注解)

Spring4.0系列5-@Conditional 

Spring4.0系列6-Generic Qualifier(泛型限定)

Spring4.0系列7-Ordering Autowired Collections

Spring4.0系列8-Groovy DSL

Spring4.0系列9-websocket简单应用

更多正在编写中。。。

 

 

 

4.0重要的一个新的改进是@RestController注解,它继承自@Controller注解。4.0之前的版本,Spring MVC的组件都使用@Controller来标识当前类是一个控制器servlet。

 

使用这个特性,我们可以开发REST服务的时候不需要使用@Controller而专门的@RestController。

 

 

当你实现一个RESTful web services的时候,response将一直通过response body发送。为了简化开发,Spring 4.0提供了一个专门版本的controller。下面我们来看看@RestController实现的定义:

 

Java代码  收藏代码
  1. @Target(value=TYPE)  
  2.  @Retention(value=RUNTIME)  
  3.  @Documented  
  4.  @Controller  
  5.  @ResponseBody  
  6. public @interface RestController  

 

 

 

官方文档解释:

 

Java代码  收藏代码
  1. 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.  
  2. 注解本身使用@Controller@ResponseBody注解。使用了这个注解的类会被看作一个controller-使用@RequestMapping的方法有一个默认的@ResponseBody注解。  
  3. @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.  
  4. @ResponseBody也可以加到类一级,通过继承方法一级不需要添加。  

 

 

 

UserDetails.java

 

 

 

Java代码  收藏代码
  1. package javabeat.net.rest;  
  2.   
  3. import javax.xml.bind.annotation.XmlAttribute;  
  4. import javax.xml.bind.annotation.XmlRootElement;  
  5. @XmlRootElement  
  6. public class UserDetails {  
  7.     private String userName;  
  8.     private String emailId;  
  9.   
  10.     @XmlAttribute  
  11.     public String getUserName() {  
  12.         return userName;  
  13.     }  
  14.     public void setUserName(String userName) {  
  15.         this.userName = userName;  
  16.     }  
  17.   
  18.     @XmlAttribute  
  19.     public String getEmailId() {  
  20.         return emailId;  
  21.     }  
  22.     public void setEmailId(String emailId) {  
  23.         this.emailId = emailId;  
  24.     }  
  25. }  

 

 SpringRestControllerDemo.java

 

 

Java代码  收藏代码
  1. package javabeat.net.rest;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.http.HttpStatus;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6. import org.springframework.web.bind.annotation.RequestMethod;  
  7. import org.springframework.web.bind.annotation.ResponseStatus;  
  8. import org.springframework.web.bind.annotation.RestController;  
  9.   
  10. @RestController  
  11. public class SpringRestControllerDemo {  
  12.     @Autowired UserDetails userDetails;  
  13.     @RequestMapping(value="/springcontent",  
  14.             method=RequestMethod.GET,produces={"application/xml""application/json"})  
  15.     @ResponseStatus(HttpStatus.OK)  
  16.     public UserDetails getUser() {  
  17.         UserDetails userDetails = new UserDetails();  
  18.         userDetails.setUserName("Krishna");  
  19.         userDetails.setEmailId("krishna@gmail.com");  
  20.         return userDetails;  
  21.     }  
  22.   
  23.     @RequestMapping(value="/springcontent.htm", method=RequestMethod.GET)  
  24.     @ResponseStatus(HttpStatus.OK)  
  25.     public String getUserHtml() {  
  26.         //Test HTML view  
  27.         return "example";  
  28.     }  
  29. }  

 

 

 

 

 dispatcher-servlet.xml

 

 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www./schema/beans"  
  3.     xmlns:mvc="http://www./schema/mvc"  
  4.     xmlns:context="http://www./schema/context"  
  5.     xmlns:xsi="http://www./2001/XMLSchema-instance"  
  6.     xsi:schemaLocation="http://www./schema/beans  
  7.   
  8. http://www./schema/beans/spring-beans-4.0.xsd  
  9.   
  10.   
  11. http://www./schema/mvc  
  12.   
  13.   
  14. http://www./schema/mvc/spring-mvc.xsd  
  15.   
  16.   
  17. http://www./schema/context  
  18.   
  19.   
  20. http://www./schema/context/spring-context-4.0.xsd">  
  21.   
  22.     <context:component-scan base-package="com.infosys.rest" />  
  23.   
  24.     <bean id="userDetails" class="javabeat.net.rest.UserDetails"/>  
  25.     <mvc:annotation-driven content-negotiation-manager="contentManager"/>  
  26.     <bean id="contentManager"  
  27.                 class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">  
  28.                 <property name="favorPathExtension" value="true"/>  
  29.                 <property name="ignoreAcceptHeader" value="true" />  
  30.                 <property name="defaultContentType" value="text/html" />  
  31.                 <property name="useJaf" value="false"/>  
  32.                 <property name="mediaTypes">  
  33.                     <map>  
  34.                         <entry key="json" value="application/json" />  
  35.                         <entry key="html" value="text/html" />  
  36.                         <entry key="xml" value="application/xml" />  
  37.                     </map>  
  38.                 </property>  
  39.         </bean>  
  40.     <bean id="jspViewResolver"  
  41.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  42.         <property name="prefix" value="/WEB-INF/jsp/" />  
  43.         <property name="suffix" value=".jsp" />  
  44.     </bean>  
  45.   
  46. </beans>  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多