0%

spring | @RequestBody

@RequestBody注解只拥有一个参数

required 默认为 true,即对象中的属性必须有一个要传,否则会抛出异常:org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing

@RequestBody注解获取的参数在请求哪?

  • post请求的requestHeaders请求头中有content-type字段,一般用来处理:applicatin/json格式的参数;
  • Spring中的@RequestBody注解是用来接收请求体中的参数数据,即requestBody请求体中,故不受参数数据长度的限制;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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());
}
1
id = 1, name = yc, age = 23

案例

1
2
3
4
5
6
7
8
public class UserEntity {
private String name;
private String password;
private String nickName;
private String email;
private String token;
private String erc20_address;
}

如果是 postman 提出的数据的话,只有 name 有值,其它都是 null

请我喝杯咖啡吧~