@PathVariable @RequestParam @RequestBody等参数绑定注解详解
一、分类
handler method 参数绑定常用的注解,我们根据他们处理的Request的内容不同分为四类:
- 处理request uri 部分的注解: @PathVariable;(这里指uri template中variable,不含queryString部分)
- 处理request header部分的注解: @RequestHeader, @CookieValue;
- 处理request body部分的注解:@RequestParam, @RequestBody;
- 处理attribute类型是注解: @SessionAttributes, @ModelAttribute;
二、主要注解详解
1、@PathVariable
当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。
下面代码把URI template 中变量 ownerId的值和petId的值,绑定到方法的参数上。
@Controller
@RequestMapping("/owners/{ownerId}")
public class RelativePathUriTemplateController { @RequestMapping("/pets/{petId}")
public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {
// implementation omitted
}
}
2、@RequestParam
用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容。(Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型)
- RequestParam可以接受简单类型的属性,也可以接受对象类型。常用来处理简单类型的绑定。
- 提交方式为:GET、POST;
- 该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定;
@Controller
@RequestMapping("/pets")
@SessionAttributes("pet")
public class EditPetForm { // ... @RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam("petId") int petId, ModelMap model) {
Pet pet = this.clinic.loadPet(petId);
model.addAttribute("pet", pet);
return "petForm";
} // ...
3、@RequestBody
该注解常用来处理Content-Type: 不是application/x-www-form-urlencoded编码的内容,例如application/json, application/xml等;
它是通过使用HandlerAdapter 配置的HttpMessageConverters来解析post data body,然后绑定到相应的bean上的。
因为配置有FormHttpMessageConverter,所以也可以用来处理 application/x-www-form-urlencoded的内容,处理完的结果放在一个MultiValueMap<String, String>里,这种情况在某些特殊需求下使用,详情查看FormHttpMessageConverter api;
@RequestMapping(value = "/something", method = RequestMethod.PUT)
public void handle(@RequestBody String body, Writer writer) throws IOException {
writer.write(body);
}
4、@RequestParam,@RequestBody小结
- 在GET请求中,不能使用@RequestBody。
- 在POST请求,可以使用@RequestBody和@RequestParam,但是如果使用@RequestBody,对于参数转化的配置必须统一。
举个例子,在SpringMVC配置了HttpMessageConverters处理栈中,指定json转化的格式,如Date转成‘yyyy-MM-dd’,则参数接收对象包含的字段如果是Date类型,就只能让客户端传递年月日的格式,不能传时分秒。因为不同的接口,它的参数可能对时间参数有不同的格式要求,所以这样做会让客户端调用同事对参数的格式有点困惑,所以说扩展性不高。
如果使用@RequestParam来接受参数,可以在接受参数的model中设置@DateFormat指定所需要接受时间参数的格式。
另外,使用@RequestBody接受的参数是不会被Servlet转化统一放在request对象的Param参数集中,@RequestParam是可以的。
综上所述,一般情况下,推荐使用@RequestParam注解来接受Http请求参数。
参考文献:
(1)https://blog.csdn.net/walkerjong/article/details/7946109#
(2)https://www.cnblogs.com/guoyinli/p/7056146.html
@PathVariable @RequestParam @RequestBody等参数绑定注解详解的更多相关文章
- 【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
@RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言 ...
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...
- SpringMVC @RequestBody @RequestParam @PathVariable 等参数绑定注解详解
request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: http://blog.csdn.net/walkerjong/article/details/794 ...
- 11.@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: handler method ...
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解(转)
引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...
- (转)@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...
- springmvc @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...
- 转载:@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
转载自:https://blog.csdn.net/walkerjong/article/details/7946109#commentBox 因为写的很好很全,所以转载过来 引言:接上一篇文章, ...
- SpringMvc之参数绑定注解详解之二
2 consumes.produces 示例 cousumes的样例: 1 @Controller 2 @RequestMapping(value = "/pets", met ...
随机推荐
- 2.1、Softmax Regression模型
Softmax Regression模型 由于Logistics Regression算法复杂度低,容易实现等特点,在工业中的到广泛的使用,但是Logistics Regression算法主要用于处理 ...
- bootsafe64 ev.sys
Win10 安装了驱动精灵之后,直接蓝屏,再次重启,出现: 采用PE启动盘进入系统,之后将C:\WINDOWS\system32\drivers下kavbootc.sys文件删除了,重启之后就可以进入 ...
- Day 4 上午
内容提要 进制转换 高精度 数论:筛法,gcd/exgcd,逆元 进制转换 10=2^3+2^0=1010(2)10=3^2+3^0=101(3) 10进制x-->k进制:短除法 k进制x--& ...
- POJ1475 Pushing Boxes 华丽丽的双重BFS
woc累死了写了两个半小时...就是BFS?我太菜了... 刚开始以为让人预先跑一遍BFS,然后一会儿取两节加起来就好了,结果发现求出来的最短路(就是这个意思)会因箱子的移动而变化....我死了QWQ ...
- Java调度线程池ScheduleExecutorService
如果在一个ScheduleExecutorService中提交一个任务,这个任务的调度周期设置 的时间比任务本身执行的时间短的话会出现什么情况?也就是在线程调度时间已经到了 但是上次的任务还没有做完的 ...
- ActiveMQ的单节点和集群部署
平安寿险消息队列用的是ActiveMQ. 单节点部署: 下载解压后,直接cd到bin目录,用activemq start命令就可启动activemq服务端了. ActiveMQ默认采用61616端口提 ...
- pandas.read_csv()函数读取文件时,关于“header=None”影响读取列数区间的右闭合总结
对于一个没有字段名标题的数据,如data.csv 1.获取数据内容.pandas.read_csv("data.csv")默认情况下,会把数据内容的第一行默认为字段名标题. imp ...
- scrapy安装和框架内容
在cdm中:直接,pip install scrapy 有可能让你升级一下pip先,就输入这个:python -m pip install --upgrade pip 当它报错的话,看看它是缺了什么, ...
- Python+Selenium定位元素的方法
Python+Selenium有以下八种定位元素的方法: 1. find_element_by_id() eg: find_element_by_id("kw") 2. find_ ...
- VI设计对于企业文化建设的重要性
VI设计对于企业文化建设非常重要,包括企业品牌形象塑造.企业价值提炼.企业文化建设等有着非常重要的作用.VI设计的发展趋势是什么? 第一 从静态到动态 中国过去一段时间以来的VI设计,也是以一种静止和 ...