java - 发送 POST 请求或尝试更新时出现 org.springframework.http.converter.HttpMessageNotReadableException

标签 java json spring jackson

我有一个带有以下 Controller 的 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。服务器没有显示任何错误,并且 Controller 似乎没有收到请求,因为“内部”没有被打印。我正在尝试熟悉 Spring,但是我找不到此类错误的原因。我将不胜感激任何帮助。所有其他方法,如/all 都工作得很好,我尝试制作另一个播种器,但这不起作用。提前谢谢。

最佳答案

错误消息很明确,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;
    }

 }

关于java - 发送 POST 请求或尝试更新时出现 org.springframework.http.converter.HttpMessageNotReadableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57002936/

相关文章:

使用 spring-test-mvc 框架的 Spring REST Controller 测试

java - Spring:没有 Hibernate Session 绑定(bind)到线程,并且配置不允许在此处创建非事务性 session

java - 查询firestore中的数组元素并将其放入Android Studio数组(CollectionReference)(whereArrayContains)

java - PathIterator中的while循环的作用是什么?

Ubuntu 中的 java.lang.UnsatisfiedLinkError

java - 使用 JSON 和 Hibernate JPA 进行无限递归

javascript - 如果 JSON 字符串有\n,则 JSON.parse 失败

java 8 过滤一个 ListOfMap 用于检查一个键是否存在,如果存在则收集一个映射

C# 自定义 Json.NET 列表序列化

java - 在测试中注入(inject) EntityManager 时,Powermock 和 Spring 导致 ConversionException