简体   繁体   English

为什么@ModelAttribute 不像@RequestBody 那样工作?

[英]Why @ModelAttribute does not work like @RequestBody?

My goal is to set a POJO attribute with form-data .我的目标是使用form-data设置 POJO 属性。 I have a generic POJO, and when I use JSON it works as expected, so I am not sure form-data behave differently.我有一个通用的 POJO,当我使用JSON时,它按预期工作,所以我不确定form-data行为是否不同。

eg例如

The POJO POJO

public class Account_Info {
  @JsonProperty("name")
  private String firstName;

  public String getFirstName() {
    return firstName;
  }
}

I wish to send a form data with "name" as key.我希望发送以“名称”为键的表单数据。

let formData = new FormData();

formData.append("name", "a");

My expected result is: The POJO's attribute of firstName will have a value of a .我的预期结果是: POJO 的firstName属性的值为a

My actual result is: The POJO's attribute of firstName is null.我的实际结果是:POJO 的firstName属性是 null。

What I did:我做了什么:

  1. I created a Account_InfoDto.我创建了一个 Account_InfoDto。
public class Account_InfoDto {
  @JsonProperty("name")
  private String firstName;

  public String getFirstName() {
    return firstName;
  }
}
  1. I created a controller我创建了一个 controller

with @RequestBody@RequestBody

  @PostMapping(value = "/account_info")
  public Account_InfoDto postAccount_Info(@RequestBody Account_InfoDto account_info) {
    return account_info;
  }

with @ModelAttribute使用@ModelAttribute

  @PostMapping(value = "/account_info", consumes = "multipart/form-data")
  public Account_InfoDto postAccount_Info(@ModelAttribute Account_InfoDto account_info) {
    return account_info;
  }
  1. Send a request to POST /account_info向 POST /account_info 发送请求

with JSONJSON

{
  "name": "a"
}

The actual response is:实际的反应是:

{
  "name": "a"
}

with form-data带有form-data

let formData = new FormData();

formData.append("name", "a");

The actual result is:实际结果是:

{
  "name": null
}

@ModelAttribute is used with view templates such as thymeleaf, and @RequestBody is used when you expect json payload, @JsonProperty is for mapping, it is not needed for formdata. @ModelAttribute用于 thymeleaf 等视图模板,@ @RequestBody在您期望 json 有效负载时使用, @JsonProperty用于映射,formdata 不需要它。 Since you are using formdata via ajax, you do not need any annotation on your payload.由于您通过 ajax 使用表单数据,因此您不需要在有效负载上进行任何注释。 the following code以下代码

@PostMapping(value = "/account_info")
public Account_InfoDto postAccount_Info(@RequestBody Account_InfoDto account_info) {
return account_info;
}

should be应该

@PostMapping(value = "/account_info")
public Account_InfoDto postAccount_Info(Account_InfoDto account_info) {
return account_info;
}

also your request should be你的要求也应该是

let formData = new FormData();

formData.append("firstName", "a");

for your DTO also include setters and getters为您的 DTO 还包括 setter 和 getter

public class Account_InfoDto {
@JsonProperty("name")
private String firstName;

public String getFirstName() {
   return firstName;
 }
public void setFirstName(String firstName) {
   this.firstName=firstName;
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一起使用@RequestBody和@ModelAttribute? - Using @RequestBody and @ModelAttribute together? @ModelAttribute在Spring框架中如何工作? - How does @ModelAttribute work in Spring framework? 为什么将@Valid与@ModelAttribute一起使用会导致异常? - Why does using @Valid with @ModelAttribute result in exception? Spring 3.1.0 MVC绑定模型属性与requestbody - Spring 3.1.0 mvc binding modelattribute along with requestbody @ModelAttribute 方法处理 @RequestBody 对象抛出 HttpMessageNotReadableException - @ModelAttribute method handles @RequestBody object throws HttpMessageNotReadableException 在 Spring MVC 中结合 @ModelAttribute 和 @RequestBody 的功能 - Combine functionality of @ModelAttribute and @RequestBody in Spring MVC 为什么@RequestBody不需要arg构造函数? - Why does @RequestBody require no arg constructor? Spring MVC @ModelAttribute @SessionAttributes-为什么模型属性需要带@ModelAttribute注释的方法? - Spring MVC @ModelAttribute @SessionAttributes - Why does a model attribute need a @ModelAttribute annotated method? 为什么这个质数会这样工作? - Why does this prime number work like this? 为什么在 Spring MVC 3.0 中使用 @ModelAttribute 'update()' 不重定向到 'sox'? - Why does 'update()' not redirect to 'sox' using @ModelAttribute in Spring MVC 3.0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM