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

hac*_*ang 15 java spring-boot

我正在学习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/"
}
Run Code Online (Sandbox Code Playgroud)

我的实体:

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
Run Code Online (Sandbox Code Playgroud)

我的控制器:

public class TopicController {

    @Autowired
    private TopicService topicService;


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

我的服务:

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

}
Run Code Online (Sandbox Code Playgroud)

我的json:

{
    "id": "3",
    "name": "topic3",
    "author": "Jessie3",
    "desc": "T3"
}
Run Code Online (Sandbox Code Playgroud)

请帮忙 !

gly*_*ing 26

对于反序列化目的,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

}     
Run Code Online (Sandbox Code Playgroud)

这是Jackson库的默认行为.


And*_*eas 9

您需要使用以下注释构造函数@JsonCreator

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

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

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

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

像这样:

@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;
}
Run Code Online (Sandbox Code Playgroud)