我有一个带有以下控制器的Spring应用程序:
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/office")
public class OfficeController {
private OfficeRepository officeRepository;
public OfficeController(OfficeRepository officeRepository){
this.officeRepository = officeRepository;
}
@GetMapping("/all")
public List<Office> getAll(){
List<Office> offices = this.officeRepository.findAll();
return offices;
}
@PutMapping
public void insert(@RequestBody Office office){
this.officeRepository.insert(office);
}
@PostMapping
public void update(@RequestBody Office office){
this.officeRepository.save(office);
}
@GetMapping("/{id}")
public Office getByIdOffice(@PathVariable("id") String id){
Office office = this.officeRepository.findById(id);
return office;
}
@GetMapping("/status/{status}")
public List<Office> getByStatus(@PathVariable("status") String status){
List<Office> offices = this.officeRepository.findByStatus(status);
return offices;
}
@GetMapping("/floor/{floor}")
public List<Office> getByFloor(@PathVariable("floor") String floor){
List<Office> offices = this.officeRepository.findByFloor(floor);
return offices;
}
}
第二类是:
public class User {
//@Id
//private String id;
private String firstName;
private String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
// public String getId() {
// return id;
// }
public String getFirstName() {
return firstName;
}
public String getLastName(){
return lastName;
}
}
但是,当我从邮递员发送包含以下内容的POST请求时:
public void run(String... strings) throws Exception {
Office bir = new Office(
"2",
"busy",
"12/07/2019",
"20/07/2019",
Arrays.asList(
new User("Jon", "Snow"))
);
我收到以下答复:
{
"timestamp": 1562918471785,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Could not read document: Can not construct instance of office.demo.User: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)\n at [Source: java.io.PushbackInputStream@85534e1; line: 8, column: 17] (through reference chain: office.demo.Office[\"users\"]->java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of office.demo.User: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)\n at [Source: java.io.PushbackInputStream@85534e1; line: 8, column: 17] (through reference chain: office.demo.Office[\"users\"]->java.util.ArrayList[0])",
"path": "/office"
}
此外,我也在尝试使用Spring Security。服务器未显示任何错误,并且由于未打印“内部”,控制器似乎未收到请求。我正在尝试熟悉Spring,但是找不到这种错误的原因。我将不胜感激。所有其他方法(例如/ all)都可以正常工作,我尝试制作另一个播种机,但这对.N无效。
答案 0 :(得分:1)
错误消息明确,User
缺少默认构造函数。如果显式声明任何构造函数,则您有责任添加No Arg构造函数。因为杰克逊默认使用setters
和getters
进行序列化和反序列化
public class User {
//@Id
//private String id;
private String firstName;
private String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public User() {
}
// public String getId() {
// return id;
// }
public String getFirstName() {
return firstName;
}
public String getLastName(){
return lastName;
}
}