spring4.0之三:@RestController

Posted 割肉机

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring4.0之三:@RestController相关的知识,希望对你有一定的参考价值。

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

 

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

技术分享图片
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {

    /**
     * The value may indicate a suggestion for a logical component name,
     * to be turned into a Spring bean in case of an autodetected component.
     * @return the suggested component name, if any
     * @since 4.0.1
     */
    String value() default "";

}
技术分享图片

官方文档解释:

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;  
    }  
  
    @XmlAttribute  
    public String getEmailId() {  
        return emailId;  
    }  
    public void setEmailId(String emailId) {  
        this.emailId = emailId;  
    }  
}  
技术分享图片
 SpringRestControllerDemo.java
技术分享图片
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("[email protected]");  
        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" />  
    <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> 
技术分享图片

 

 

以上是关于spring4.0之三:@RestController的主要内容,如果未能解决你的问题,请参考以下文章

项目ITP spring4.0 整合 Quartz 实现动态任务调度

Spring4.0 整合Quartz2.2.1

Spring4.0系列5-@Conditional

Spring4.0学习笔记 —— 自动装配

Spring4.0实战 rest相关

spring4.0 之 @Configuration注解