【Spring Boot】3.@RequestParam和@RequestBody的不同&用法示例

1. @RequestParam

该标签用于接受请求头中的参数,该标签配置了四个参数:

public @interface RequestParam {

	@AliasFor("name")
	String value() default "";
	
	@AliasFor("value")
	String name() default "";

	boolean required() default true;
	
	String defaultValue() default ValueConstants.DEFAULT_NONE;
}

  • valuename,用来接收请求的参数,与请求参数的 Key 值相互绑定。(注:@AliasFor 表示别名,它可以注解到自定义注解的两个属性上,表示这两个互为别名,也就是说这两个属性其实同一个含义)
  • required,表示该请求参数是否为必须传入,默认为 true,即必须传入。
  • defaultValue,设置请求参数传入为空,或没有传入该参数时,使用的默认值,当然对应 required 参数需要设置为 false。

2. @RequestBody

该标签用于接受请求中的请求体,该标签只有一个参数:

public @interface RequestBody {
	boolean required() default true;
}
  • required,与 @RequestParam 标签相同,表示该请求参数是否为必须传入,这里指是否需要传递请求体,默认为 true,即必须传入。

3. 不同点

3.1 处理的请求的 Content-Type 不同

  • @RequestParam 可处理 Content-Type 为 application/x-www-form-urlencoded 编码的内容的请求,Content-Type默认为该属性。@RequestParam 也可用于其它类型的请求,例如:POST、DELETE等请求。
  • @RequestBody 接收的参数是来自 requestBody 中,即请求体。一般用于处理非 Content-Type: application/x-www-form-urlencoded 编码格式的数据,比如:application/json 等类型的数据。
  • @RequestParam 和 @RequestBody 分别适用于表单提交以及 json 提交方式过来的数据

3.2 GET请求

  • @RequestParam 可以相应 GET 请求。
  • 由于 GET 请求没有请求体,@RequestBody 不用于该场景。

4. 用法示例

4.1 @RequestParam

4.1.1 接收 URL 上的参数

例如获取下图 URL 中,后面带的参数
在这里插入图片描述
代码:通过两个 @RequestParam 指定属性的 Key 获取相应的值

	@GetMapping(value = "test1")
    @ResponseBody
    public String test1(@RequestParam(value = "token") String token,
                       @RequestParam(value = "accountId") String accountId) {
        System.out.println("token: " + token + "\naccountId" + accountId);
        return "hello world";
    }

4.1.2 接收 application/x-www-form-urlencoded 请求参数

请求头:
在这里插入图片描述
请求体:
在这里插入图片描述
代码:

	@PostMapping(value = "test2")
    @ResponseBody
    public String test2(@RequestParam(value = "length") String length,
                        @RequestParam(value = "md5") String md5) {
        System.out.println("length: " + length + "\nmd5: " + md5);
        return "hello world";
    }

4.2 @RequestBody

4.2.1 实体类映射 JSON 对象

这里先构建一个实体类用于接收请求:

public class Person {
    private Integer age;
    private String name;
    private Double weight;
	// 这里省略 get set 方法以及构造函数
}

构建接收请求的方法,直接将实体类使用 @RequestBody 标签,会自动将 JSON 数据封装为实体类:

	@PostMapping(value = "test3")
    @ResponseBody
    public String test3(@RequestBody Person person) {
        return person.toString();
    }

发送请求时,只需要指定:Content-Type 为 application/json,再直接拼接 JSON 即可:
在这里插入图片描述

4.2.1 Map 映射 JSON

但是,对于每个请求,我们有时候不一定都可以构建一个实体类与之对应,这时候可以采用 Map 的方式,进行接收,例如我们还是上面例子中传入的 JSON 数组,这时的 JAVA 代码可以改变为:

	@PostMapping(value = "test4")
    @ResponseBody
    public String test4(@RequestBody Map<String, String> map) {
        Person person = new Person();
        person.setAge(Integer.parseInt(map.get("age")));
        person.setName(map.get("name"));
        person.setWeight(Double.parseDouble(map.get("weight")));
        return person.toString();
    }

4.3 @RequestParam & @RequestBody

这两个类型的标签,是可以同时使用的,例如在 URL 中添加参数,同时在 RequestBody 中新增 JSON 数组:
在这里插入图片描述
接收请求的代码:

	@PostMapping(value = "test5")
    @ResponseBody
    public String test5(@RequestParam(value = "token") String token,
                        @RequestParam(value = "accountId") String accountId,
                        @RequestBody Map<String, String> map) {
        Person person = new Person();
        person.setAge(Integer.parseInt(map.get("age")));
        person.setName(map.get("name"));
        person.setWeight(Double.parseDouble(map.get("weight")));
        return person.toString();
    }

以上为对两个标签的整理,相关 Demo 代码已上传 github

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章