SpringMvc之参数绑定注解详解之一
引言:
前段时间项目中用到了REST风格来开发程序,但是当用POST、PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加 任何注解),查看了提交方式为application/json, 而且服务器端通过request.getReader() 打出的数据里确实存在浏览器提交的数据。为了找出原因,便对参数绑定(@RequestParam、 @RequestBody、 @RequestHeader 、 @PathVariable)进行了研究,同时也看了一下HttpMessageConverter的相关内容,在此一并总结。
简介:
@RequestMapping
RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
RequestMapping注解有六个属性,下面我们把她分成三类进行说明。
1、 value, method;
value: 指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);
当之设置value一个属性时,value可以省略不写,当有其他属性时则需要加上进行区分,如下图:

method: 指定请求的method类型, GET、POST、PUT、DELETE等;
2、 consumes,produces;
consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
3、 params,headers;
params: 指定request中必须包含某些参数值是,才让该方法处理。
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。
示例:
1、value / method 示例
默认RequestMapping("....str...")即为value的值;

1 @Controller
2 @RequestMapping("/appointments")
3 public class AppointmentsController {
4
5 private final AppointmentBook appointmentBook;
6
7 @Autowired
8 public AppointmentsController(AppointmentBook appointmentBook) {
9 this.appointmentBook = appointmentBook;
10 }
11
12 @RequestMapping(method = RequestMethod.GET)
13 public Map<String, Appointment> get() {
14 return appointmentBook.getAppointmentsForToday();
15 }
16
17 @RequestMapping(value="/{day}", method = RequestMethod.GET)
18 public Map<String, Appointment> getForDay(@PathVariable @DateTimeFormat(iso=ISO.DATE) Date day, Model model) {
19 return appointmentBook.getAppointmentsForDay(day);
20 }
21
22 @RequestMapping(value="/new", method = RequestMethod.GET)
23 public AppointmentForm getNewForm() {
24 return new AppointmentForm();
25 }
26
27 @RequestMapping(method = RequestMethod.POST)
28 public String add(@Valid AppointmentForm appointment, BindingResult result) {
29 if (result.hasErrors()) {
30 return "appointments/new";
31 }
32 appointmentBook.addAppointment(appointment);
33 return "redirect:/appointments";
34 }
35 }

value的uri值为以下三类:
A) 可以指定为普通的具体值;
B) 可以指定为含有某变量的一类值(URI Template Patterns with Path Variables);
C) 可以指定为含正则表达式的一类值( URI Template Patterns with Regular Expressions);
example B)
1 @RequestMapping(value="/owners/{ownerId}", method=RequestMethod.GET)
2 public String findOwner(@PathVariable String ownerId, Model model) {
3 Owner owner = ownerService.findOwner(ownerId);
4 model.addAttribute("owner", owner);
5 return "displayOwner";
6 }
example C)
1 @RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\d\.\d\.\d}.{extension:\.[a-z]}")
2 public void handle(@PathVariable String version, @PathVariable String extension) {
3 // ...
4 }
5 }
SpringMvc之参数绑定注解详解之一的更多相关文章
- SpringMvc之参数绑定注解详解之三
2. @RequestHeader.@CookieValue @RequestHeader 注解,可以把Request请求header部分的值绑定到方法的参数上. 示例代码: 这是一个Request ...
- SpringMvc之参数绑定注解详解之二
2 consumes.produces 示例 cousumes的样例: 1 @Controller 2 @RequestMapping(value = "/pets", met ...
- SpringMvc之参数绑定注解详解
引言: 前段时间项目中用到了REST风格来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没有加任何注解),查看了提交方式为application/j ...
- SpringMvc之参数绑定注解详解之四
简介: @RequestBody 作用: i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对 ...
- 【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
@RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言 ...
- @PathVariable @RequestParam @RequestBody等参数绑定注解详解
一.分类 handler method 参数绑定常用的注解,我们根据他们处理的Request的内容不同分为四类: 处理request uri 部分的注解: @PathVariable;(这里指ur ...
- SpringMVC @RequestBody @RequestParam @PathVariable 等参数绑定注解详解
request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: http://blog.csdn.net/walkerjong/article/details/794 ...
- springmvc @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...
随机推荐
- liferay 指定默认首页
1.登录liferay后,点击控制面板-->设置--> portal设置 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmVuZ3hpbmdf ...
- 图形数据库(GraphDB)
Date: 2016-01-27 Title: 图形数据库-1 Published: true Type: post Excerpt: Category: GraphDB 图形数据库(GraphDB) ...
- persisted? vs new_record?
https://joe11051105.gitbooks.io/you-need-to-know-about-ruby-on-rails/content/activerecord/persisted_ ...
- Java多线程系列 JUC锁06 Condition条件
Condition介绍 Condition中提供了一组类似于Object中的监视器方法.与Lock配合可以完成等待通知模式. Lock lock = new ReentrantLock(); Cond ...
- [原创]java WEB学习笔记11:HttpServlet(HttpServletRequest HttpServletRsponse) 以及关于 Servlet 小结
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- hbase shell-dml(数据管理指令)
hbase shell数据管理篇: append count delete deleteall get get_counter get_splits incr put scan truncate tr ...
- 《机器学习实战》学习笔记第十一章 —— Apriori算法
主要内容: 一.关联分析 二.Apriori原理 三.使用Apriori算法生成频繁项集 四.从频繁项集中生成关联规则 一.关联分析 1.关联分析是一种在大规模数据集中寻找有趣关系的任务.这些关系可以 ...
- 通用jquery页面验证
页面通过给input 标签设置样式或者属性,即可实现所有页面的验证,customValidate方法并支持自己补充验证! 验证包括:必输项,只能输入数字, 输入数字并设定小数最大位数,下拉框必选,复选 ...
- LSM Tree 学习笔记——MemTable通常用 SkipList 来实现
最近发现很多数据库都使用了 LSM Tree 的存储模型,包括 LevelDB,HBase,Google BigTable,Cassandra,InfluxDB 等.之前还没有留意这么设计的原因,最近 ...
- python-编译安装Python2.7
yum中最新的也是Python 2.6.6,只能下载Python 2.7.5的源代码自己编译安装. 操作步骤如下: 1)下载并解压Python 2.7.9的源代码 cd /opt wget --no- ...