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. Bugly集成指南

    官网: https://bugly.qq.com/v2/,用QQ扫码登录即可 1.创建应用,获取APPID 2.自动集成 2.1 在Module的build.gradle文件中添加依赖和属性配置: d ...

  2. Data Base mysql批量操作

    mysql  批量操作 批量操作数据是利用 CommandBuilder  和 DataAdapter.Update() 方法 对数据库进行批量更新 说解: DataAdapter中有四个重要对象:S ...

  3. 在RobotFramework--RIDE中把日期转化为整型进行运算

    在RobotFramework--RIDE中把日期转化为整型进行运算 运行结果: 20180906 16:10:17.919 : INFO : ${time} = 2018-09-06 16:10:1 ...

  4. tomcat - 认识

    tomcat - web应用服务器 环境:ubuntu测试 @shell命令(cd到tomcat目录下) 启动: ./bin  startup.sh 关闭:./bin  shutdown.sh @部署 ...

  5. vue_cli下开发一个简单的模块权限系统之展现数据

    这个页面是用户列表:userList就是第二张截图中的data里面的userList vue中只要改变存放数据的载体就会实现页面改变,mounted的意思是页面加载时执行这里面的函数,我们需要在页面加 ...

  6. (转)Jmeter参数化

    设置参数值的方法有如下几种: 1. 用Jmeter中的函数获取参数值,__Random,__threadNum,__CSVRead,__StringFromFile,具体调用方法如下:${__Rand ...

  7. luogu2658 GCD(莫比乌斯反演/欧拉函数)

    link 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 1<=N<=10^7 (1)莫比乌斯反演法 发现就是YY的GCD,左转YY的GCD ...

  8. loj#6229. 这是一道简单的数学题 (??反演+杜教筛)

    题目链接 题意:给定\(n\le 10^9\),求:\(F(n)=\sum_{i=1}^n\sum_{j=1}^i\frac{\mathrm{lcm}(i,j)}{\mathrm{gcd}(i,j)} ...

  9. 10大Python开源项目推荐(Github平均star2135)

    翻译 | suisui 来源 | 人工智能头条(AI_Thinker) 继续假日充电系列~本文是 Mybridge 挑选的 10 个 Python 开源项目,Github 平均star 2135,希望 ...

  10. 【三支火把】---# program (n)以及C语言字符对齐的总结

    #pragma pack(n) 当n大于结构体中内存占用最大的变量内存时,将按照最大内存变量的字节占用数进行对齐,否则,就根据n进行对齐 情况一: 例一: #pragma pack(4) struct ...