【问题标题】:org.springframework.http.converter.HttpMessageNotReadableException when sending a POST request or trying to update发送 POST 请求或尝试更新时出现 org.springframework.http.converter.HttpMessageNotReadableException
【发布时间】: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 等所有其他方法都运行良好,我尝试制作另一个播种机,但效果不佳。提前致谢。

【问题讨论】:

标签: java json spring jackson


【解决方案1】:

错误信息很清楚,User 缺少默认构造函数。如果您显式声明任何构造函数,那么您有责任添加 No Arg 构造函数。因为jackson默认使用settersgetters进行序列化和反序列化

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;
    }

 }

【讨论】:

    猜你喜欢
    • 2016-06-02
    • 1970-01-01
    • 2020-11-20
    • 2022-08-03
    • 2017-06-08
    • 2018-03-25
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    相关资源
    最近更新 更多