Skip to content
Advertisement

Bad Request from saving structured JSON object

I have created a simple web REST application that has to save a “change ticket” to database. But after I try to create a POST request with JSON as body I get an error:

JavaScript

I am using Spring Boot 2, MySQL as DB and MapStruct to convert DTO to entities and vice versa. Also I use Project lombok to get rid of some boilerplate code

My controller POST method is depicted here:

JavaScript

My entity which I am saving looks like this:

JavaScript

Superclass:

JavaScript

And here is the sample JSON code I am sending via POSTMAN to as POST request to: http://localhost:8080/change/save

JavaScript

I don’t know if it is a problem with Jackson not knowing how to deserialize/serialize child and parent objects or If I am building my Json wrongly. If you will need to see my repositories of service classes I will edit this post to show them.

EDIT: adding ChangeTicketDTO

JavaScript

BaseItemDTO

JavaScript

Thanks for help guys

Advertisement

Answer

I am back with a solution that worked for me. I have tried to test and see what can cause this problem and it turned out, that this problem occurs when you construct your JSON request body wrong.

I was sending POST request to my controller with JSON body as you can see up in my post. But than I got the idea to write a test method that will transform my object to JSON so I will be sure that my JSON is constructed in a proper way. You can see it here:

JavaScript

This method returned a simple JSON string that looked like this:

JavaScript

So I have created another POST request to the same URL and it worked!!! So now I know that if I will create a bad JSON that is not corresponding to my objectt(and its parent super class) I will get this kind of error. Thanks a lot

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