Skip to content
Advertisement

I am trying to create an API using SpringBoot but I don’t know how to handle json request/response

I am new to Java and Spring boot. I am creating a new API. Using postman I am sending a request body which contains request header and request payload.

enter image description here

Then I have a controller which handles the request with the help of RequestPayload class. (And a service and dao file but I am sure those are ok.)

Kindly let me know what Am I missing here or what do I not know.

JavaScript

Advertisement

Answer

You need a POJO class that will match the structure of your JSON payload, actually a few nested classes. Spring will automatically parse JSON into this POJO.

JavaScript

Then add it to Controller as an argument like this:

JavaScript

This tutorial explains it well https://www.baeldung.com/spring-request-response-body

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