Spring4.0系列3-@RestController

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.springframework.org/schema/beans"  
  3.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.   
  8. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
  9.   
  10.   
  11. http://www.springframework.org/schema/mvc  
  12.   
  13.   
  14. http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  15.   
  16.   
  17. http://www.springframework.org/schema/context  
  18.   
  19.   
  20. http://www.springframework.org/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>  
时间: 2024-08-02 09:54:57

Spring4.0系列3-@RestController的相关文章

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

在Spring4.0里,泛型是可以用来决定哪一个bean需要依赖注入的(无论xml配置还是注解配置). 用一个简单的例子解释.假设你有一个使用了泛型的DAO.   Java代码   public class Dao<T> {     ...   }    现在创建两个实现类的bean: Java代码   import org.springframework.context.annotation.Bean;   import org.springframework.context.annotat

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

spring框架自2.0开始添加注解的支持,之后的每个版本都增加了更多的注解支持.注解为依赖注入,AOP(如事务)提供了更强大和简便的方式.这也导致你要是用一个相同的注解到许多不同的类中去.这篇文章介绍meta annotation来解决这个问题. Meta Annotation(元注解)定义 Spring4.0的许多注解都可以用作meta annotation(元注解).元注解是一种使用在别的注解上的注解.这意味着我们可以使用Spring的注解组合成一个我们自己的注解. 创建组合注解 自定义注

Spring4.0系列7-Ordering Autowired Collections

spring 4.0的一个小特性是在自动注入的时候使用@Order.Spring 2.5中,我们将bean注入List,如下代码: Java代码   import org.springframework.stereotype.Component;   @Component   public class Employee implements Person {   }     Java代码   import org.springframework.stereotype.Component;   

Spring4.0系列8-Groovy DSL

4.0的一个重要特征就是完全支持Groovy,Groovy是spring主导的一门基于JVM的脚本语言(动态语言).在spring 2.x,脚本语言通过 Java scripting engine在Spring中得到支持.而在4.0中,Groovy的变得更重要,Groovy可以替换xml和注解用来作为bean配置. 要使用Groovy,首先用maven下载Groovy的包,pom.xml文件中添加: Xml代码   <dependency>    <groupId>org.code

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

spring 4.0的一个最大更新是增加了websocket的支持.websocket提供了一个在web应用中的高效.双向的通讯,需要考虑到客户端(浏览器)和服务器之间的高频和低延时消息交换.一般的应用场景有:在线交易.游戏.协作.数据可视化等.   使用websocket需要考虑的浏览器的支持(IE<10不支持),目前主流的浏览器都能很好的支持websocket. websocket协议中有一些子协议,可以从更高的层次实现编程模型,就像我们使用HTTP而不是TCP一样.这些子协议有STOMP,

Spring4.0系列5-@Conditional

这篇文章介绍spring 4的@Conditional注解.在Spring的早期版本你可以通过以下方法来处理条件问题: 3.1之前的版本,使用Spring Expression Language(SPEL). 3.1版本有个新特性叫profile,用来解决条件问题. 1.Spring Expression Language(SPEL) SPEL有一个三元运算符(if-then-else)可以在配置文件中当作条件语句,如下: Java代码   <bean id="flag">

[CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口

Reprint it anywhere u want. 文章Points: 1.介绍RESTful架构风格 2.Spring配置CXF 3.三层初设计,实现WebService接口层 4.撰写HTTPClient 客户端,并实现简单调用     介绍RESTful架构风格     REST是REST之父Roy Thomas创造的,当时提出来了REST的6个特点:客户端-服务器的.无状态的.可缓存的.统一接口.分层系统和按需编码.其具有跨语言和跨平台的优势.     REST是一种架构风格.其描述

ActionScript 3.0系列教程(4):爽快使用XML

xml|教程 ActionScript 3.0系列教程(3):Document Class特色为我们带来了什么? ActionScript 3.0系列教程(4):爽快使用XML 为什么放弃AS2.0选择AS3.0?如果只允许我说三个理由.那么AS3.0对XML的近乎完美的支持绝对是其中一个. 简单说说AS3.0中对于XML支持的不同吧: .AS2.0对XML的支持勉勉强强,将就着可以用.而AS3.0中对XML的支持是全方位的,极其强大和灵活的. AS2.0对XML的支持不是内建的(build-i

Flash ActionScript 3.0系列教程

教程 作者的blog: www.kingda.org ActionScript 3.0系列教程(1):与Flash9先来一次亲密接触! Flash Professional 9 ActionScript 3.0 Preview 版本今天发布了,意味着从此我们从此不仅仅只能使用Flex 2来使用AS3.0,更可以使用我们一直很熟悉的Flash IDE来进行AS3.0开发了. 与Flex 2不同,Flash 9 alpha(即上面的Flash Professional 9 ActionScript