2

spring | @RequestBody

 1 year ago
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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK