@RequestParam:接收来自RequestHeader中,即请求头。通常用于GET请求,例如:http://localhost:8080/hello/name=admin&age=18
- @Target({ElementType.PARAMETER})
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- public @interface RequestParam {
- @AliasFor("name")
- String value() default "";
-
- @AliasFor("value")
- String name() default "";
-
- boolean required() default true;
-
- String defaultValue() default "\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";
- }
- @GetMapping("/hello")
- public String hello(@RequestParam(name = "id") Long id){
- System.out.println("hello " + id);
- return "hello message";
- }
@RequestParam用来处理
Content-Type为application/x-www-form-undencoded编码的内容,Content-Type默认为该属性@RequestParam也可用于其它类型的请求,例如:POST、DELETE等请求。
由于@RequestParam是用来处理 Content-Type 为 application/x-www-form-urlencoded 编码的内容的,所以在postman中,要选择body的类型为 x-www-form-urlencoded,这样在headers中就自动变为了 Content-Type : application/x-www-form-urlencoded 编码格式。如下图所示:
- @PostMapping("/save")
- public String hello(@RequestParam(name = "id") Long id,
- @RequestParam("name") String name,
- @RequestParam("password") String password){
- System.out.println(user);
- return "hello message";
- }

如果前台传递过来的参数不是三个,而是十个,如果继续使用 @RequestParam 的方式来接收请求参数,就需要十个 @RequestParam ,我们的代码可读性将会变得很差,并且当参数类型相同时,十分容易出错。所以使用实体类来接收传递过来的参数,但是 @RequestParam 不支持直接传递实体类的方式
- @Data
- public class User {
-
- @NotNull(message = "id不能为空")
- private Long id;
-
- @NotBlank(message = "名字不能为空")
- private String name;
-
- @NotBlank(message = "密码不能为空")
- private String password;
- }
- // @RequestParam 不支持直接传递实体类的方式,所以可以在实体类中做数据校验,使用 @Validated 注解使得作用在实体类属性上的注解生效
- @PostMapping("/save")
- public String save(@Validated User user){
- System.out.println(user);
- return "hello success";
- }
- // console result: User(id=110, name=admin, password=123456)

如果改用 json 字符串来传值的话,类型设置为 application/json,点击发送的话,会报错,后台接收不到值,为 null

// console result: User(id=null, name=null, password=null)
注解@RequestBody接收的参数是来自requestBody中,即请求体。一般用于处理非 Content-Type: application/x-www-form-urlencoded编码格式的数据,比如:application/json、application/xml等类型的数据。
就application/json类型的数据而言,使用注解@RequestBody可以将body里面所有的json数据传到后端,后端再进行解析。
- @PostMapping("/saveBatch")
- public String saveBatch(@RequestBody @Validated List
list) { - list.forEach(System.out::println);
- return "saveBatch success";
- }

- // console result:
- // User(id=1, name=admin, password=123456)
- // User(id=2, name=cheny, password=cheny)
- @PostMapping("/listMap")
- public String saveMap(@RequestBody List{
- for (Map
map : listMap) { - System.out.println(map);
- }
- return "listMap success";
- }

- // console result:
- // {id=1, name=admin}
- // {id=2, age=18}
注解@RequestParam接收的参数是来自requestHeader中,即请求头。通常用于GET请求,像POST、DELETE等其它类型的请求也可以使用。
注解@RequestBody接收的参数是来自requestBody中,即请求体。一般用于处理非 Content-Type: application/x-www-form-urlencoded编码格式的数据,比如:application/json、application/xml等类型的数据。通常用于接收POST、DELETE等类型的请求数据,GET类型也可以适用。
举个例子,在SpringMVC配置了HttpMessageConverters处理栈中,指定json转化的格式,如Date转成‘yyyy-MM-dd’,则参数接收对象包含的字段如果是Date类型,就只能让客户端传递年月日的格式,不能传时分秒。因为不同的接口,它的参数可能对时间参数有不同的格式要求,所以这样做会让客户端调用同事对参数的格式有点困惑,所以说扩展性不高。
如果使用@RequestParam来接受参数,可以在接受参数的model中设置@DateFormat指定所需要接受时间参数的格式。
另外,使用@RequestBody接受的参数是不会被Servlet转化统一放在request对象的Param参数集中,@RequestParam是可以的。
综上所述,一般情况下,推荐使用@RequestParam注解来接受Http请求参数。
参考文献
注解@RequestParam与@RequestBody的使用场景 - 腾讯云开发者社区-腾讯云
@RequestBody和@RequestParam区别全面详细_一斤白菜的博客-CSDN博客_@requestbody和@requestparam