一、分类

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等参数绑定注解详解的更多相关文章

  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. SpringMVC @RequestBody @RequestParam @PathVariable 等参数绑定注解详解

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

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

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

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

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

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

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

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

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

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

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

  9. SpringMvc之参数绑定注解详解之二

    2 consumes.produces 示例 cousumes的样例: 1 @Controller   2 @RequestMapping(value = "/pets", met ...

随机推荐

  1. docker下ubutun没有ifconfig命令问题

    解决: apt-get update #更新apt-get apt install net-tools       # ifconfig apt install iputils-ping     # ...

  2. JavaWeb学习笔记(十三)—— JDBC时间类型的处理

    一.Java中的时间类型 Java中用类java.util.Date对日期/时间做了封装,此类提供了对年.月.日.时.分.秒.毫秒以及时区的控制方法,同时也提供一些工具方法,比如日期/时间的比较,前后 ...

  3. git 各个区的区别

    Git有三大区(工作区.暂存区.版本库)以及几个状态(untracked.unstaged.uncommited) 把文件往Git版本库里添加的时候,是分两步执行的: 第一步是用 git add 把文 ...

  4. C++_类和动态内存分配4-有关返回对象的说明

    返回方式: 返回指向对象的引用: 指向对象的const引用: const对象:  =============================================== 返回指向const对象 ...

  5. C++_友元2-友元成员函数

    接着上一篇<友元是什么>中,我们发现Remote友元类的大多数方法都是用Tv类的公有接口实现.这意味着这些方法并不是真正需要友元. 事实上唯一直接访问Tv成员的Remote方法是Remot ...

  6. java.math.BigDecimal cannot be cast to java.lang.String

    从数据库总查询出的count(*) 函数统计的值,类型转换方法: Map<String,Integer> map = new HashMap<String,Integer>() ...

  7. LeetCode902. Numbers At Most N Given Digit Set

    题目: We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}.  ...

  8. Codeforce-1106-D. Lunar New Year and a Wander(DFS遍历+vector存图+set)

    Lunar New Year is approaching, and Bob decides to take a wander in a nearby park. The park can be re ...

  9. 113th LeetCode Weekly Contest Reveal Cards In Increasing Order

    In a deck of cards, every card has a unique integer.  You can order the deck in any order you want. ...

  10. i2c设备驱动之设备地址

    第一步:查找设备的数据手册可得到设备的从机地址.读写地址. 很不巧的是我在这里又卡了近一天,由于自己的硬件知识学得相当的那啥,哎,没办法,怨不得别人. 今天终于开窍了!!!!! 在开始条件(S)后,发 ...