Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
931 views
in Technique[技术] by (71.8m points)

java - JSON parse error: Can not construct instance of io.starter.topic.Topic

Im learning Spring Boot and I made a demo but when I POST a request to add a Object it didn't work!

The error message is:

{
    "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?)
 at [Source: java.io.PushbackInputStream@1ff3f09a; line: 2, column: 9]",
    "path": "/topics/"
}

My Entity:

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

My controller:

public class TopicController {

    @Autowired
    private TopicService topicService;


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

My service:

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

}

My json:

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

Please Help !

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For deserialisation purposes Topic must have a zero-arg constructor.

For example:

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

}     

This is the default behaviour of the Jackson library.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...