1、Spring对REST的支持

Spring3(这里讨论Spring3.2+)对Spring MVC的一些增强功能为REST提供了良好的支持。Spring对开发REST资源提供以下支持:

  1. 操作方式:控制器可以处理所有的HTTP方法,包含4个主要的REST方法:GET、PUT、DELETE以及POST。Spring的表单绑定JSP标签库的<form:form>标签以及新的HiddenHttpMethodFilter,使得通过HTML表单提交的PUT和DELETE请求成为可能
  2. 资源标识:新的@PathVariable注解使得控制器能够处理参数化的URL
  3. 资源表述方法1:通过使用Spring的视图解析器,资源可以以各种形式进行表述,包括XML、JSON、Atom和RSS()。使用新的ContentNegotiatingViewResolver来选择最合适客户端的表述
  4. 资源表述方法2:通过使用@ResponseBody注解和各种HttpMethodConverter实现来达到。@RequestBody注解记忆HttpMethodConverter实现可以将传入的HTTP数据转化为传入控制器方法的Java对象
  5. REST客户端:RestTemplate简化了客户端对REST资源的使用

2、参数化URL

Spring3.0中引入了新的@PathVariable注解,帮助Controller使用面向资源的方式处理请求。如:

 1 @Controller
2 @RequestMapping(value="/spitters")
3 public class SpittleController{
4 private SpitterService spitterService;
5 ...
6 @RequestMapping(value="/{id}", method= RequestMethod.PUT)
7 @ResponseStatus(HttpStatus.NO_CONTENT)
8 public void putSpittle(@PathVariable("id")) long id,
9 @Valid Spittle spittle)
10 {
11 spitterService.saveSpittle(spittle);
12 }
13 }

3、协商资源表述

“协商资源表述”即确定一个HTTP请求所需要返回的资源格式,HTML、XML还是JSON等。按优先级,Spring MVC依次通过以下三个条件确定客户端需要什么类型的内容表述:

  1. URL路径上添加扩展名,如 http://myserver/myapp/accounts/list.html 或者 http://myserver/myapp/accounts/list.xls。
  2. URL参数,如 http://myserver/myapp/accounts/list?format=xls,参数名默认为“format”,但也可以更改。
  3. 通过HTTP请求的Accept头部信息确定返回资源格式,但由于客户端的处理不同,这个方法并不太可靠。

JAF(JavaBeans Activation Framework)可提供由扩展名(或URL参数)到资源格式的自动映射机制(如json->"application/json"),这时classpath下必须有activation.jar包。

Spring MVC通过ContentNegotiationManager来进行资源协商,一般配置如下:

 1 <bean id="contentNegotiationManager"
2 class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
3 <property name="favorPathExtension" value="false" />
4 <property name="favorParameter" value="true" />
5 <property name="parameterName" value="mediaType" />
6 <property name="ignoreAcceptHeader" value="true"/>
7 <property name="useJaf" value="false"/>
8 <property name="defaultContentType" value="application/json" />
9
10 <property name="mediaTypes">
11 <map>
12 <entry key="json" value="application/json" />
13 <entry key="xml" value="application/xml" />
14 </map>
15 </property>
16 </bean>

以上配置表示:不使用路径扩展名方式,也不使用Accept头信息方式,仅使用URL参数方式确定资源类型,URL参数名使用“mediaType”代替默认的“format”,不使用JAF而是自行定义URL参数到资源格式的映射,这里只定义了JSON和XML。

4、表述资源(一)

Spring MVC通过两种方式将Java表述形式的资源转化为发送给客户端的表述形式:

  • 基于视图渲染进行协商(@ContentNegotiatingViewResolver)
  • HTTP消息转换器(HttpMessageConverters)

首先是第一种方式,使用HttpMessageConverters表述资源。当ContentNegotiationManager配置如下,且类路径下包含JAXB和Jackson包时,Spring MVC将自动匹配使用哪种HttpMessageCoverter。

ContentNegotiationManager配置:

 1 <!-- enable the "produces" annotation of "RequestMapping" -->
2 <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
3
4 <!-- Simple strategy: only path extension is taken into account -->
5 <bean id="cnManager"
6 class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
7 <property name="favorPathExtension" value="true"/>
8 <property name="ignoreAcceptHeader" value="true" />
9 <property name="defaultContentType" value="text/html" />
10 <property name="useJaf" value="false"/>
11
12 <property name="mediaTypes">
13 <map>
14 <entry key="html" value="text/html" />
15 <entry key="json" value="application/json" />
16 <entry key="xml" value="application/xml" />
17 </map>
18 </property>
19 </bean>

Controller:

 1 @Controller
2 class AccountController {
3 // RESTful method, use MappingJacksonHttpMessageConverter and Jaxb2RootElementHttpMessageConverter automatically
4 //accounts.json -> application/json
5 //accounts.xml -> text/xml
6 @RequestMapping(value="/accounts", produces={"application/xml", "application/json"})
7 @ResponseStatus(HttpStatus.OK)
8 public @ResponseBody List<Account> listWithMarshalling(Principal principal) {
9 return accountManager.getAccounts(principal);
10 }
11
12 // View-based method
13 //accounts.html -> text/html
14 //accounts.others -> text/html
15 @RequestMapping("/accounts")
16 public String listWithView(Model model, Principal principal) {
17 // Call RESTful method to avoid repeating account lookup logic
18 model.addAttribute( listWithMarshalling(principal) );
19
20 // Return the view to use for rendering the response
21 return ¨accounts/list¨;
22 }
23 }

使用 JAXB 和 Jackson时,需要在account类上添加annotation:

 1 /**
2 * Represents an account for a member of a financial institution. An account has
3 * zero or more {@link Transaction}s and belongs to a {@link Customer}. An aggregate entity.
4 */
5 @Entity
6 @Table(name = "T_ACCOUNT")
7 @XmlRootElement
8 public class Account {
9
10 // data-members omitted ...
11
12 public Account(Customer owner, String number, String type) {
13 this.owner = owner;
14 this.number = number;
15 this.type = type;
16 }
17
18 /**
19 * Returns the number used to uniquely identify this account.
20 */
21 @XmlAttribute
22 public String getNumber() {
23 return number;
24 }
25
26 /**
27 * Get the account type.
28 *
29 * @return One of "CREDIT", "SAVINGS", "CHECK".
30 */
31 @XmlAttribute
32 public String getType() {
33 return type;
34 }
35
36 /**
37 * Get the credit-card, if any, associated with this account.
38 *
39 * @return The credit-card number or null if there isn't one.
40 */
41 @XmlAttribute
42 public String getCreditCardNumber() {
43 return StringUtils.hasText(creditCardNumber) ? creditCardNumber : null;
44 }
45
46 /**
47 * Get the balance of this account in local currency.
48 *
49 * @return Current account balance.
50 */
51 @XmlAttribute
52 public MonetaryAmount getBalance() {
53 return balance;
54 }
55
56
57 /**
58 * Returns a single account transaction. Callers should not attempt to hold
59 * on or modify the returned object. This method should only be used
60 * transitively; for example, called to facilitate reporting or testing.
61 *
62 * @param name
63 * the name of the transaction account e.g "Fred Smith"
64 * @return the beneficiary object
65 */
66 @XmlElement // Make these a nested <transactions> element
67 public Set<Transaction> getTransactions() {
68 return transactions;
69 }
70
71 // Setters and other methods ...
72
73 }

5、表述资源(二) 

第二种方式,使用视图渲染器(View Resolution)。使用ContentNegotiatingViewResolver和ContentNegotiationManager进行配置:

 1 <!-- View resolver that delegates to other view resolvers based on the content type -->
2 <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
3 <!-- All configuration is now done by the manager - since Spring V3.2 -->
4 <property name="contentNegotiationManager" ref="cnManager"/>
5 </bean>
6
7 <!-- Setup a simple strategy -->
8 <bean id="cnManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
9 <property name="ignoreAcceptHeader" value="true"/>
10 <property name="defaultContentType" value="text/html" />
11 </bean>

使用以上配置Spring将根据约定自动查找ViewResolvers,并渲染资源。也可显式配置ViewResolvers:

 1 <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
2 <property name="contentNegotiationManager" ref="cnManager"/>
3
4 <!-- Define the view resolvers explicitly -->
5 <property name="viewResolvers">
6 <list>
7 <bean class="org.springframework.web.servlet.view.XmlViewResolver">
8 <property name="location" value="spreadsheet-views.xml"/>
9 </bean>
10
11 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
12 <property name="prefix" value="WEB-INF/views"/>
13 <property name="suffix" value=".jsp"/>
14 </bean>
15 </list>
16 </property>
17 </bean>

这样Controller就可以去掉@ResponseBody的Method,只写一个Method了。

6、参考

《Spring in Action 第三版》

Content Negotiation using Spring MVC

Content Negotiation using Views

SpringMVC restful风格的更多相关文章

  1. SpringMVC RESTful风格URL处理带点的参数

    使用了RESTful风格的URL.但是在处理带点的搜索参数,比如baidu.com的时候,后台就取不到正确的参数了. 搜索了下原因,原来是SpringMVC将点号看作扩展分隔符号了,因此后台没法拿到正 ...

  2. springmvc restful风格操作

    ssm框架 controller: package com.sgcc.controller; import java.util.ArrayList; import java.util.List; im ...

  3. SpringMVC restful风格下载文件,文件后缀被截掉

    原文:https://blog.csdn.net/wisdomhealth/article/details/78400421 @RequestMapping("/file/{filename ...

  4. Restful风格API接口开发springMVC篇

    Restful风格的API是一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机 ...

  5. SpringMVC实现Restful风格的WebService

    1.环境 JDK7 MyEclipse2014 tomcat8 maven 3.3.3 spring4.1.4 2.创建maven工程 使用MyEclipse创建maven工程的方式可以参考这篇博文( ...

  6. [五]SpringMvc学习-Restful风格实现

    1.Restful风格的资源URL 无后缀资源的访问(csdn用法) 2.SpringMvc对Rest风格的支持 2.1将 /*.do改为/ 2.2 3.@PathVariable获取Url变量 @R ...

  7. SpringMVC 构建Restful风格 及问题处理

    基本的请求URL: /person/{id}  GET  得到id的person /person POST      新增person /person/{id}  PUT  更新id的person / ...

  8. SpringMvc笔记-对RESTFUL风格的配置

    1.@RequestMapping注解可以使用如下参数: 1,params:例如params={'username',"age!=100"}表示需要usernmame并且age 属 ...

  9. springMVC中添加restful 风格

    RESTful架构:是一种设计的风格,并不是标准,只是提供了一组设计原则和约束条件,也是目前比较流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网站的采用. 关于 ...

随机推荐

  1. C 标签使用

    JSTL 核心标签库标签共有13个,功能上分为4类: 1.表达式控制标签:out.set.remove.catch 2.流程控制标签:if.choose.when.otherwise 3.循环标签:f ...

  2. 【转】C#对XML文件的各种操作实现方法

    [转]C#对XML文件的各种操作实现方法 原文:http://www.jb51.net/article/35568.htm XML:Extensible Markup Language(可扩展标记语言 ...

  3. 42、生鲜电商平台-商品的spu和sku数据结构设计与架构

    说明:Java开源生鲜电商平台中商品的spu和sku数据结构设计与架构,包括数据库图标与架构分析. 1. 先说明几个概念. 电商网站采用在商品模块,常采用spu+sku的数据结构算法,这种算法可以将商 ...

  4. Web开发秘方(WEB DEVELOPMENT RECIPES)[47.5MB] PDF扫描版

    不借助插件怎样在移动设备上实现动画效果?怎样快速搭建HTML电子邮箱?怎样制作跨PC和移动设备显示的应用界面?怎样利用最新的JavaScript框架提高应用的响应速度?怎样有效利用CoffeeScri ...

  5. Web性能权威指南 PDF扫描版​

    Web性能权威指南是谷歌公司高性能团队核心成员的权威之作,堪称实战经验与规范解读完美结合的产物.<Web性能权威指南>目标是涵盖Web开发者技术体系中应该掌握的所有网络及性能优化知识.全书 ...

  6. [C# 线程处理系列]专题四:线程同步

    目录: 一.线程同步概述 二.线程同步的使用 三 .总结 一.线程同步概述 前面的文章都是讲创建多线程来实现让我们能够更好的响应应用程序,然而当我们创建了多个线程时,就存在多个线程同时访问一个共享的资 ...

  7. ubuntu 删除mysql

    UBUNTU 彻底删除 MYSQL 然后重装 MYSQL 删除 mysql sudo apt-get autoremove --purge mysql-server-5.0sudo apt-get r ...

  8. MasterPage + UpdatePanel + FileUpload

    上传文件在母版页与Ajax的UpdatePanel的环境进行.由于在母版内使用Ajax,建议使用AjaxControlToolkit.dll组件,去微软官网下载后,并拉入BIN目录中. 然后去web. ...

  9. shell脚本实现自动保留最近n次备份记录

    项目中出现的问题 某天上午服务器出现卡顿特别严重,页面加载速度奇慢,并且某些页面刷新出现404的问题,就连服务器的tab命令的自动提示都出现了问题,楼主费了九牛二虎之力,根据服务器排查发现,服务器数据 ...

  10. windows windows计划任务访问网络存储NAS的问题

    windows 计划任务访问网络存储NAS的问题 WINDOWS 计划任务访问网络存储(NAS),程序找不到路径解决办法 Windows 服务器端运行的软件,大多时需要开机自启,这时Windows 计 ...