当前位置: 代码迷 >> 综合 >> Spring 的 @RequestBody和@RequestParam
  详细解决方案

Spring 的 @RequestBody和@RequestParam

热度:33   发布时间:2024-01-17 22:11:05.0

@RequestParam
用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容。
(Http协议中,如果不指定Content-Type,则默认传递的参数就是application/x-www-form-urlencoded类型)

RequestParam可以接受简单类型的属性,也可以接受对象类型。 
实质是将Request.getParameter() 中的Key-Value参数Map利用Spring的转化机制ConversionService配置,转化成参数接收对象或字段。

在Content-Type: application/x-www-form-urlencoded的请求中, 
get 方式中queryString的值,和post方式中 body data的值都会被Servlet接受到并转化到Request.getParameter()参数集中,所以@RequestParam可以获取的到。

@RequestBody
处理HttpEntity传递过来的数据,一般用来处理非Content-Type: application/x-www-form-urlencoded编码格式的数据(比如:不会对参数编码multipart/form-data)。


Get不能使用表单,只能在url中传参,传参方式只有这一种。 (因为没有HttpEntity,所以@RequestBody不适用。)
Post通过HttpEntity传递的参数,可以使用表单(@RequestParam、@RequestBody),也可以在url中传参(没有?)(@PathVariable&#

  相关解决方案