Skip to content
Advertisement

Jackson cannot parse body of a POST request

I am using Spring Boot to implement two web services, User and Book, and I am implementing the POST methods. When I send a POST request, i include the hard-coded JSON object in the HTTP Body: I am working at the same time on the two services, and while the creation of the “user” object works fine, Jackson throws an exception for the Book class. This situation occurres also with Postman, so it is not a problem of the JSON creation.

This is the Book class

JavaScript

While this is the User one:

JavaScript

And these are the JSON objects in the body, printed when they are sent from the client When I send a POST for creating a user, it’s all good

JavaScript

While for the book, the WS returns error 500

JavaScript

And this exception is thrown

JavaScript

Advertisement

Answer

In your Book class you have 2 constructors meanwhile in User just has 1. Jackson might not know which constructors to invoke when deserializing your Book. Adding @JsonCreator annotation to your constructor might work.

http://fasterxml.github.io/jackson-annotations/javadoc/2.5/com/fasterxml/jackson/annotation/JsonCreator.html

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement