背景


昨天一个人瞎倒腾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. 第一天 :学习node.js

    ① node.js环境配置 我学过的语言最简单的一门 直接百度就可以配置 ② 每个入门 的程序都是从helloworld开始 代码如下 : var http=require('http'); http ...

  2. 全站 HTTPS 来了(转载)

    转载:本文为腾讯Bugly原创文章. 最近大家在使用百度.谷歌或淘宝的时候,是不是注意浏览器左上角已经全部出现了一把绿色锁,这把锁表明该网站已经使用了 HTTPS 进行保护.仔细观察,会发现这些网站已 ...

  3. shell中的语法(1)

    反引号 命令替换.将命令的输出放在命令行的任意位置. eg. [root@gam ~]# echo The Data is `date` The Data is Fri Nov 18 10:13:56 ...

  4. js点击打开一个固定宽高的网页

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. JAVA-系统-【3】-java应用连接oracle正常,但是网页却报错java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

    这是因为jar包已经导入了项目的应用部分,网页的lib去没有导入. 导入以后还要刷新项目,最好重启tomcat,就解决了..

  6. Foundation ----->NSSet

    1.集合类     NSString *s1 = @"zhangsan";     NSString *s2 = @"lisi";     NSString * ...

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

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

  8. java网络流传输,中文乱码问题。

    最近需要从某个网页上抓取数据.一波三折. 1. 先要找到网站页面调用后台数据服务的url地址,但是本人对js不了解,花了不少时间在分析其网页源代码的js部分,试图寻找出调用数据的链接. 后来得知浏览器 ...

  9. WPF 大数据加载过程中的等待效果——圆圈转动

    大家肯定遇到过或将要遇到加载大数据的时候,如果出现长时间的空白等待,一般人的概念会是:难道卡死了? 作为一个懂技术的挨踢技术,即使你明知道数据量太大正在加载,但是假如看不到任何动静,自己觉得还是一种很 ...

  10. windbg学习---.browse打开一个新的command 窗口

    .browse r eax .browse <command>将会显示新的命令浏览窗口和运行给出的命令