背景


昨天一个人瞎倒腾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. osx 编译安装配置 ruby on rails

    下载源代码: curl -O http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.2.tar.gz 解压: .tar.gz 编译: cd ruby- ./ ...

  2. 使用Plsql将Excel数据导入Oracle数据库

    1.在plsql页面,Tools-->ODBC Importer... 2.配置被导入的excel files 3.选择excel文件 4.选择Excel中要被导入的的sheet工作簿,选择之后 ...

  3. Neural Network学习(二)Universal approximator :前向神经网络

    1. 概述 前面我们已经介绍了最早的神经网络:感知机.感知机一个非常致命的缺点是由于它的线性结构,其只能做线性预测(甚至无法解决回归问题),这也是其在当时广为诟病的一个点. 虽然感知机无法解决非线性问 ...

  4. 动态组合lambda 表达式

    //记录实体集合—动态组合lambda 表达式 Expression<Func<AdEntity, bool>> thirdWhere = p => p.Observer ...

  5. svn更新操作时提示database is locked

    If you're on Windows version just let's do the next: Right click on the repo folder and go to Tortoi ...

  6. Content has been consumed

    if(response.getEntity() != null && response.getEntity().getContent() != null) { message = IO ...

  7. 通过Dockerfile建立.NET Core mvc Image

    生成.NET core mvc code docker run -itd microsoft/dotnet:latestdocker psdocker attach containeridmkdir ...

  8. poj1157LITTLE SHOP OF FLOWERS

    Description You want to arrange the window of your flower shop in a most pleasant way. You have F bu ...

  9. dma驱动

    http://www.crifan.com/files/doc/docbook/dma_pl08x_analysis/release/html/dma_pl08x_analysis.html#idp2 ...

  10. python学习笔记4-redis multi watch实现锁库存

    python 关于redis的基本操作网上已经很多了,这里主要介绍点个人觉得有意思的内容1.redis的事务操作以及watch 乐观锁:后面描述2.tornado下异步使用redis的方式       ...