2
spring | @RequestBody
source link: https://benpaodewoniu.github.io/2023/01/30/spring41/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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
请求体中,故不受参数数据长度的限制;
@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
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK