我无法将来自post方法体的JSON转换为我的POJO,即使在控制器类中使用了@RequestBody
。
我调试了错误,并发现某些字段被映射,而其他字段则没有。就像这个(POJO):
name: null, typeOfPlan: null, Email: example@gmail.com, PhoneNum: 123456789, Website: test.org, Username: null, password: 1234
,这很奇怪。
JSON:
{
"confirmPassword": "1234",
"email": "example@gmail.com",
"password": "1234",
"phoneNum": "123456789",
"name": "Hello world",
"typeOfPlan": "Test",
"userName": "user",
"website": "test.org"
}
控制器
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SomeController {
@Autowired
private Service foo;
@CrossOrigin
@PostMapping(value = "/create")
private void createAccount(@RequestBody BigFoo bigFoo) {
foo.createAccount(bigFoo);
}
}
从这里开始,我调用我的服务,然后调用DAO类。
POJO(普通Java对象)
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class BigFoo {
private String name;
private String typeOfPlan;
private String email;
private String website;
private String phoneNum;
private String username;
private String password;
}
我还尝试在@PostMapping
中使用consumes media type
以允许JSON,但它未能解决这个问题。
使用Jackson ObjectMapper也无法解决问题。