Spring @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
背景
昨天一个人瞎倒腾spring boot,然后遇到了一点问题,所以把这个问题总结一下。
主要讲解request 数据到handler method 参数数据的绑定,所用到的注解和什么情形下使用。
正文
注解名 | 应用场景描述 | 调用示例 | 示例代码 |
---|---|---|---|
@PathVariable | 当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上 |
GET [host]/users/{id} |
@GetMapping(value = "/{id}") public User getUserById(@PathVariable Long id) { return users.get(id); } |
@RequestHeader | @RequestHeader 注解, 可以把Request请求header部分的值绑定到方法的参数上 |
http request head Host localhost:8080 Accept application/json Accept-Encoding gzip,deflate Keep-Alive 300 |
@GetMapping public String getChartset(@RequestHeader("Accept-Encoding") String encoding) { //… } |
@CookieValue | @CookieValue 可以把Request header中关于cookie的值绑定到方法的参数上 | cookie值 JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84 |
@GetMapping("/displayHeaderInfo") public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie) { //… } |
@RequestParam | A) 常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况; (String–> 简单类型的转换操作由ConversionService配置的转换器来完成) 因为使用request.getParameter()方式获取参数,所以可以处理 GET 方式中queryString的值,也可以处理POST 方式中 body data的值;B) 用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式 GET、POST ;C) 该注解有两个属性: value、required; value:用来指定要传入值的id名称; required:用来指示参数是否必须绑定; |
GET [host]/users?id={id} |
@GetMapping(value = "/{id}") public User getUserById(@RequestParam Long id) { return users.get(id); } |
@RequestBody | 该注解常用来处理Content-Type: 不是application/x-www-form-urlencoded编码的内容,例如application/json, application/xml等; 它是通过使用HandlerAdapter 配置的HttpMessageConverters来解析post data body,然后绑定到相应的bean上的。 |
PUT [host]:8080/users/3 RequestBody { "name": "小鸭", "age": 23 } |
@PutMapping(value = "/{id}") public String updateUser(@PathVariable Long id, @RequestBody User user) { //… } |
@ModelAttribute | 该注解有两个用法,一个是用于方法上,一个是用于参数上; 用于方法上时: 通常用来在处理@RequestMapping之前,为请求绑定需要从后台查询的model; 用于参数上时: 用来通过名称对应,把相应名称的值绑定到注解的参数bean上;要绑定的值来源于: A) @SessionAttributes 启用的attribute 对象上; B) @ModelAttribute 用于方法上时指定的model对象; C) 上述两种情况都没有时,new一个需要绑定的bean对象,然后把request中按名称对应的方式把值绑定到bean中。 |
POST [host]/users/1/aaa/11 |
@PostMapping(value = "/{id}/{name}/{age}") public String addUser(@ModelAttribute User user) { //… } |
Spring @RequestParam @RequestBody @PathVariable 等参数绑定注解详解的更多相关文章
- 【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
@RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言 ...
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...
- @RequestParam @RequestBody @PathVariable 等参数绑定注解详解(转)
引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...
- 转载:@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
转载自:https://blog.csdn.net/walkerjong/article/details/7946109#commentBox 因为写的很好很全,所以转载过来 引言:接上一篇文章, ...
- 11.@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: handler method ...
- (转)@RequestParam @RequestBody @PathVariable 等参数绑定注解详解
引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...
- springmvc @RequestParam @RequestBody @PathVariable 等参数绑定注解详解
简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...
- SpringMVC @RequestBody @RequestParam @PathVariable 等参数绑定注解详解
request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: http://blog.csdn.net/walkerjong/article/details/794 ...
- @PathVariable @RequestParam @RequestBody等参数绑定注解详解
一.分类 handler method 参数绑定常用的注解,我们根据他们处理的Request的内容不同分为四类: 处理request uri 部分的注解: @PathVariable;(这里指ur ...
随机推荐
- 拒绝访问 temp 目录。用来运行 XmlSerializer 的标识“IIS APPPOOL\UGAS”没有访问 temp 目录的足够权限
在部署IIS时候会出现下图错误,拒绝访问 temp 目录.用来运行 XmlSerializer 的标识“IIS APPPOOL\UGAS”没有访问 temp 目录的足够权限 解决方法: 在IIS信息管 ...
- 图解GitHub基本操作
目录 一.注册并登陆到github网站 1.1.打开github网站首页(https://github.com/) 1.2.注册一个自己的github账号 1.3.登陆自己的github账号 二.创建 ...
- 关于mysql数据库字符集优先级问题
mysql数据库可以分别设置数据库字符集.表字符集和表字段字符集. 1.数据库字符集 < 表字符集 < 表字段(列)字符集. 例如数据库字符集为gbk -- GBK Simplified ...
- CVPR 2011 Global contrast based salient region detection
Two salient region detection methods are proposed in this paper: HC AND RC HC: Histogram based contr ...
- TJI读书笔记16-异常处理
TJI读书笔记16-异常处理 概念 基本异常情形 异常的捕获 自定义异常 异常说明 捕获所有异常 栈轨迹 重新抛出异常 Java标准异常 使用finally 异常的限制 构造器 异常的匹配 其他乱七八 ...
- XidianOJ 1020 ACMer去刷题吧
题目描述 刷题是每个ACMer必由之路,已知某oj上有n个题目,第i个题目小X能做对的概率为Pi(0<=Pi<=1,1<=i<=n) 求小X至少做对k道题的概率 输入 第一行输 ...
- 【231】◀▶ 利用 IDL 读取 TIFF 数据
参考:Create Latitude/Longitude Arrays for GeoTIFF Image 用到的函数为 READ_TIFF,通过此函数可以获取 TIFF 数据的数组信息,同时可以获取 ...
- mysql 用source 导入数据库报错
平时一直使用phpmyadmin或mysqldum进行导出,使用source命令导入数据库. 但换了新版本mysql后,上述导入方法出现以下错误: ERROR: Unknown command '\\ ...
- 位段(bitfield)
struct { unsigned int fieldA : 4 ; unsigned int fieldB : 2 ; unsigned int ...
- python install_opener用法
opener:当你获取一个URL时,你使用一个opener(OpenerDirector).正常情况下我们一直使用默认的opener,通过urlopen,但你也可以创建自定义的openers. url ...