`
wiselyman
  • 浏览: 2080858 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
博客专栏
Group-logo
点睛Spring4.1
浏览量:81077
74ae1471-94c5-3ae2-b227-779326b57435
点睛Spring MVC4...
浏览量:130130
社区版块
存档分类
最新评论

Spring4.0系列3-@RestController

阅读更多

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实现的定义:

 

@Target(value=TYPE)
 @Retention(value=RUNTIME)
 @Documented
 @Controller
 @ResponseBody
public @interface RestController

 

 

 

官方文档解释:

 

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("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" />

	<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>

 

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


2
4
分享到:
评论
6 楼 somefuture 2015-11-23  
string2020 写道
@RestController和@Controller   有什么区别?

RestController下面的action返回都默认为ResponseBody
5 楼 string2020 2015-03-13  
@RestController和@Controller   有什么区别?
4 楼 zjhdreams 2014-09-18  
表示没有怎么看懂
3 楼 zm9913 2014-07-11  
 @Autowired UserDetails userDetails;  
这一行代码是什么作用?
2 楼 wiselyman 2014-03-19  
java_0331 写道
你好,请问如果我不想再user实体中用注解,如何让spring4返回xml呢?

那建议你使用json返回值
1 楼 java_0331 2014-02-25  
你好,请问如果我不想再user实体中用注解,如何让spring4返回xml呢?

相关推荐

Global site tag (gtag.js) - Google Analytics