【发布时间】:2019-07-12 08:18:17
【问题描述】:
我有一个带有以下控制器的 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;
}
}
但是,当我从 Postman 发送一个包含以下内容的 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 等所有其他方法都运行良好,我尝试制作另一个播种机,但效果不佳。提前致谢。
【问题讨论】:
-
我发现您在 User 类中没有设置器,并且构造函数的 @JsonCreator 注释可能会起作用。以防万一 - stackoverflow.com/questions/21920367/…