JSON 解析错误:无法构造 io.starter.topic.Topic 的实例

2022-09-01 13:40:14

我正在学习Spring Boot,我做了一个演示,但是当我发布添加对象的请求时,它不起作用!

错误消息是:

{
    "timestamp": 1516897619316,
    "status": 400,
    "error": "Bad Request",
    "exception": "org.springframework.http.converter.HttpMessageNotReadableException",
    "message": "JSON parse error: Can not construct instance of io.starter.topic.Topic: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of io.starter.topic.Topic: 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@1ff3f09a; line: 2, column: 9]",
    "path": "/topics/"
}

我的实体:

public class Topic {
    private String id;
    private String name;
    private String author;
    private String desc;

    public Topic(String id, String name, String author, String desc) {

        this.id = id;
        this.name = name;
        this.author = author;
        this.desc = desc;
    }
    //getters and setters

我的控制器:

public class TopicController {

    @Autowired
    private TopicService topicService;


    @RequestMapping(value = "/topics", method = RequestMethod.POST)
    public void addTopic(@RequestBody Topic topic) {
        topicService.addTopic(topic);
    }

我的服务:

@Service
public class TopicService {
    private List<Topic> topics = new ArrayList<>(Arrays.asList(
            new Topic("1", "topic1", "Martin", "T1"),
            new Topic("2", "topic2", "Jessie", "T2")  
            ));



    public void addTopic(Topic topic) {

        topics.add(topic);
    }

}

我的 json:

{
    "id": "3",
    "name": "topic3",
    "author": "Jessie3",
    "desc": "T3"
}

请帮忙!


答案 1

出于反序列化目的,必须具有零参数构造函数。Topic

例如:

public class Topic {
    private String id;
    private String name;
    private String author;
    private String desc;

    // for deserialisation
    public Topic() {}    

    public Topic(String id, String name, String author, String desc) {    
        this.id = id;
        this.name = name;
        this.author = author;
        this.desc = desc;
    }

    // getters and setters

}     

这是杰克逊库的默认行为。


答案 2

您需要使用@JsonCreator对构造函数进行批注:

标记批注,可用于将构造函数和工厂方法定义为用于实例化关联类的新实例的标记批注。

注意:在注释创建者方法(构造函数、工厂方法)时,方法必须是:

  • 没有JsonProperty参数注释的单参数构造函数/工厂方法:如果是这样,这就是所谓的“委托创建者”,在这种情况下,Jackson首先将JSON绑定到参数的类型中,然后调用创建者。这通常与JsonValue(用于序列化)结合使用。
  • 构造函数/工厂方法,其中每个参数都使用 JsonPropertyJacksonInject 进行批注,以指示要绑定到的属性的名称

另请注意,所有JsonProperty注释都必须指定实际名称(“默认”不是空字符串),除非您使用可以检测参数名称的扩展模块之一;这是因为 8 之前的默认 JDK 版本无法从字节码中存储和/或检索参数名称。但是对于JDK 8(或者使用辅助库,如Paranamer,或其他JVM语言,如Scala或Kotlin),指定名称是可选的。

喜欢这个:

@JsonCreator
public Topic(@JsonProperty("id") String id, @JsonProperty("name") String name,
             @JsonProperty("author") String author, @JsonProperty("desc") String desc) {
    this.id = id;
    this.name = name;
    this.author = author;
    this.desc = desc;
}

推荐