背景


昨天一个人瞎倒腾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 等参数绑定注解详解的更多相关文章

  1. 【转】@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    @RequestParam @RequestBody @PathVariable 等参数绑定注解详解 2014-06-02 11:24 23683人阅读 评论(2) 收藏 举报 目录(?)[+] 引言 ...

  2. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...

  3. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解(转)

    引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...

  4. 转载:@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    转载自:https://blog.csdn.net/walkerjong/article/details/7946109#commentBox   因为写的很好很全,所以转载过来 引言:接上一篇文章, ...

  5. 11.@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: handler method ...

  6. (转)@RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后,该篇主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: 简介: han ...

  7. springmvc @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    简介: handler method 参数绑定常用的注解,我们根据他们处理的Request的不同内容部分分为四类:(主要讲解常用类型) A.处理requet uri 部分(这里指uri templat ...

  8. SpringMVC @RequestBody @RequestParam @PathVariable 等参数绑定注解详解

    request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: http://blog.csdn.net/walkerjong/article/details/794 ...

  9. @PathVariable @RequestParam @RequestBody等参数绑定注解详解

    一.分类 handler method 参数绑定常用的注解,我们根据他们处理的Request的内容不同分为四类: 处理request uri 部分的注解:   @PathVariable;(这里指ur ...

随机推荐

  1. sybase ODBC驱动

    windows64位系统ODBC数据源管理器位置 64位 C:\Windows\System32\odbcad32.exe 32位 C:\Windows\SysWOW64\odbcad32.exe s ...

  2. 墨卡托投影C#实现

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. SqlServer Where后面Case When语句的写法

    select * from tb where (case when col='***' then '***' else '***' end)='***'

  4. javascript运行模式:并发模型 与Event Loop

    看了阮一峰老师的JavaScript 运行机制详解:再谈Event Loop和[朴灵评注]的文章,查阅网上相关资料,把自己对javascript运行模式和EVENT loop的理解整理下,不一定对,日 ...

  5. JS 取得一个区间的随机整数

    function rnd(n, m){        var random = Math.floor(Math.random()*(m-n+1)+n);        return random;   ...

  6. 自己写的 限制文本框TEdit中只能输入数字

    procedure TForm4.Edit1KeyPress(Sender: TObject; var Key: Char); begin , #]) then begin Key := #; end ...

  7. UVa 488 - Triangle Wave

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=s ...

  8. CentOS上安装spark standalone mode(转载)

    原文链接 http://blog.csdn.net/chenxingzhen001/article/details/11072765 参考: http://spark.incubator.apache ...

  9. 忘记密码流程——UUID,AES

    忘记密码流程 1.进入忘记密码页面 2. 后台检验参数合法性(null,验证码,邮箱合法性) 3,生成更新密码链接,并将相关参数写入DB link=urlBase(baseurl)+updatePas ...

  10. lua代码设置unity对象的基础属性

    设置对象的父节点: wall.transform:SetParent(GameObject.Find("Walls").transform) 设置颜色: wall:GetCompo ...