软件编程
位置:首页>> 软件编程>> java编程>> 关于@RequestBody和@RequestParam注解的使用详解

关于@RequestBody和@RequestParam注解的使用详解

作者:夜光下丶  发布时间:2023-01-20 09:08:20 

标签:@RequestBody,@RequestParam,注解,使用

@RequestParam

@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";
}

GET请求

@GetMapping("/hello")
   public String hello(@RequestParam(name = "id") Long id){
       System.out.println("hello " + id);
       return "hello message";
   }

关于@RequestBody和@RequestParam注解的使用详解

@RequestParam用来处理Content-Typeapplication/x-www-form-undencoded编码的内容,Content-Type 默认为该属性

@RequestParam也可用于其它类型的请求,例如:POST、DELETE等请求

POST请求

由于@RequestParam是用来处理 Content-Typeapplication/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";
   }

关于@RequestBody和@RequestParam注解的使用详解

 如果前台传递过来的参数不是三个,而是十个,如果继续使用 @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)

关于@RequestBody和@RequestParam注解的使用详解

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

关于@RequestBody和@RequestParam注解的使用详解

// console result: User(id=null, name=null, password=null)

@RequestBody

注解@RequestBody接收的参数是来自requestBody中,即请求体。一般用于处理非 Content-Type: application/x-www-form-urlencoded编码格式的数据,比如:application/jsonapplication/xml等类型的数据。

application/json类型的数据而言,使用注解@RequestBody可以将body里面所有的json数据传到后端,后端再进行解析。

@PostMapping("/saveBatch")
public String saveBatch(@RequestBody @Validated List<User> list){
   list.forEach(System.out::println);
   return "saveBatch success";
}

关于@RequestBody和@RequestParam注解的使用详解

// console result:
// User(id=1, name=admin, password=123456)
// User(id=2, name=cheny, password=cheny)

传递到 Map 中

@PostMapping("/listMap")
public String saveMap(@RequestBody List<Map<String, String>> listMap){
   for (Map<String, String> map : listMap) {
       System.out.println(map);
   }
   return "listMap success";
}

关于@RequestBody和@RequestParam注解的使用详解

// console result:
// {id=1, name=admin}
// {id=2, age=18}

来源:https://blog.csdn.net/weixin_46058921/article/details/127794325

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com