Java JSON分析错误:无法构造io.starter.topic.topic的实例
我正在学习SpringBoot,我做了一个演示,但是当我发布一个添加对象的请求时,它没有工作 错误消息是:Java JSON分析错误:无法构造io.starter.topic.topic的实例,java,spring-boot,Java,Spring Boot,我正在学习SpringBoot,我做了一个演示,但是当我发布一个添加对象的请求时,它没有工作 错误消息是: { "timestamp": 1516897619316, "status": 400, "error": "Bad Request", "exception": "org.springframework.http.converter.HttpMessageNotReadableException", "message": "JSON parse e
{
"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/"
}
我的实体:
我的控制器:
我的服务:
请帮忙 为了进行反序列化,主题必须具有零arg构造函数 例如:
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
}
这是库的默认行为。您需要用以下内容注释构造函数: 标记注释,可用于将构造函数和工厂方法定义为用于实例化关联类的新实例的方法 注意:注释创建者方法(构造函数、工厂方法)时,方法必须为:
- 无参数注释的单参数构造函数/工厂方法:若有,则称为“委托创建者”,在这种情况下,Jackson首先将JSON绑定到参数类型中,然后调用创建者。这通常与(用于序列化)一起使用
- 构造函数/工厂方法,其中每个参数都用或进行注释,以指示要绑定到的属性的名称
@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;
}
谢谢我已经解决了这个问题!提供您所做的解决方案。大多数情况下,原因是,此答案中没有默认的无参数构造函数。如果FirstClass将secondClass作为属性,则此答案与下面的Andreas一起是最佳答案。第一个类在构造函数上应该有一个JsonCreator annot,该构造函数将第二个对象(或列表)作为带有JsonProperty annot的参数。第二个类需要有一个空构造函数,其中包含所有属性的getter和setter。谢谢。这解决了我的问题。:)
@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);
}
}
{
"id": "3",
"name": "topic3",
"author": "Jessie3",
"desc": "T3"
}
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
}
@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;
}