0. 前言
发现一篇讲@RequestParam
和@RequestBody
的区别的文章,感觉写的挺好。
在编写项目的过程中,老出现前后端传递参数格式不一致、不统一的问题,
@RequestParam
和@RequestBody
的区别,避免大家遭遇同等错误;
1. @RequestParam
注解
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
/**
* 参数名称(和value同等意思)
*/
@AliasFor("name")
String value() default "";
/**
* 参数名称 (和name同等意思)
*/
@AliasFor("value")
String name() default "";
/**
* 是否必选(默认必选)
*/
boolean required() default true;
/**
* 参数默认值
*/
String defaultValue() default ValueConstants.DEFAULT_NONE;
}
1.1 @RequestParam
总体上来说,该注解类拥有三个参数:
-
value
、name
属性都标识请求参数名(必须配置); -
required
:参数是否必传,默认为true
,可以设置为非必传false
;(如果设置了必传或默认,请求未传递参数,将会抛出异常); -
defaultValue
:参数默认值,如果设置了该值,required 将会自动设置为 false;
1.2 @RequestParam
注解获取的参数放在请求体的哪个部分?
-
get
请求的requestHeaders
中content-type
这个字段,使用form-data
表单形式携带参数请求; -
Spring
中的@RequestParam
注解接收的参数大多数场景是来自requestHeaders
中,即请求头,也就是url
中,格式为:http://localhost:8080?name=yc&age=23
,由于url
长度有限制,所以参数需要限制数量
和值得长度
;
1.3 如何使用:
使用一:
利用Postman
工具,使用form-data
提交Get
请求
执行代码:
@RequestMapping(value = "/test", method = RequestMethod.GET)
public void test(@RequestParam("id") Integer id,
@RequestParam("name") String name,
@RequestParam("age") Integer age) {
log.info("id = {}, name = {}, age = {}", id, name, age);
}
结果:
id = 1, name = yc, age = 23
使用二:
不使用@RequestParam
注解直接进行对象属性赋值(不推荐使用,容易和@ReuqestBody
混淆)
代码执行:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String name;
private Integer age;
}
@RequestMapping(value = "/test", method = RequestMethod.GET)
public void test(User user) {
log.info("id = {}, name = {}, age = {}", user.getId(), user.getName(), user.getAge());
}
结果:
id = 1, name = yc, age = 23
1.4 使用场景:
-
请求是为了查找资源,获取服务器数据;
-
请求结果无持续性的副作用,例如:不会对数据库进行添加、修改、删除操作;
-
传入的参数不会太长,因为
Get
请求可能会产生很长的URL
,或许会超过某些浏览器与服务器对URL
的长度限制,导致请求失败;
2. @RequestBody
注解
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestBody {
/**
* 默认参数必传
*/
boolean required() default true;
}
2.1 @RequestBody
注解只拥有一个参数:
required
默认为 true
,即对象中的属性必须有一个要传,否则会抛出异常:org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing
2.2 @RequestBody
注解获取的参数在请求哪?
-
post
请求的requestHeaders
请求头中有content-type
字段,一般用来处理:applicatin/json
格式的参数; -
Spring
中的@RequestBody
注解是用来接收请求体
中的参数数据,即requestBody
请求体中,故不受参数数据长度的限制;
2.3 如何使用?
使用Postman
工具发送json
格式的数据:
执行代码:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String name;
private Integer age;
}
@RequestMapping(value = "/test", method = RequestMethod.POST)
public void test(@RequestBody User user) {
log.info("id = {}, name = {}, age = {}", user.getId(), user.getName(), user.getAge());
}
结果:
id = 1, name = yc, age = 23
2.4 使用场景:
-
请求的结果有持续性作用,例如:对数据库添加、更新、删除操作;
-
若使用Get请求,表单参数过长;
-
要传送的数据不是采用7位的ASCII编码;
3. 使用Post
请求,@RequestParam
也可以接收参数;
注意:
也可以使用这种方式用,发送Post
请求,参数拼接到url
之后,这是因为协议之间没有做严格的区分,但这种方式不建议使用,这种方式就使用Get
方式即可。例如:localhost:8888/optimus-prime/project/test?id=1&name=yc&age=23
使用浏览器请求数据,这种方式Get请求,但后端使用Post方式接收,访问不成功!
执行代码:
@PostMapping(value = "/test")
public void test(@RequestParam("id") Integer id,
@RequestParam("name") String name,
@RequestParam("age") Integer age) {
log.info("id = {}, name = {}, age = {}", id, name, age);
}
结果:
id = 1, name = yc, age = 12
4. 额外知识1:
Http
协议常用的四种请求方式:Post、Get、Put、Delete
等;其中Put、Delete
请求方式很少见,都可用Post
方式代替!
-
对数据库而言:
get
请求不修改数据库,只是查询。Post
是增加记录,put
是更新,Delete
数据库删除; -
Put,Post,Delete
方式的请求参数会直接放在requestBody
里; -
处理
request uri
部分的注解,路径参数变量:@PathVariable
; -
处理
request header
部分的注解:@RequestHeader, @CookieValue,@RequestParam
; -
处理
request body
部分的注解:@RequestParam, @RequestBody
;
综上所述:
@RequestParam
注解既可以接收Get
方式的请求头中的参数,也可以接收Post
方式的请求体中的参数;
5. 额外知识2:
get
请求的 headers
中没有 content-type
这个字段,post
的 content-type
有 :
-
application/x-www-form-urlencoded
这种就是一般的文本表单用post
传地数据,只要将得到的data
用@RequestParam
或request.getParamter()
获取即可; -
multipart/form-data
,用于文件上传,此时form
的enctype
属性必须指定为multipart/form-data
; -
application/json
,将数据以json
对象的格式传递; -
text/xml
; -
put
和delete
请求的headers
是有content-type
这个字段的,只不过这两个方法类型目前不常用;