Spring Boot - Consuming and Producing JSON
Last Updated :
23 Jul, 2025
Spring Boot is one of the famous frameworks for developing web applications and this framework provides a lot of features like auto-configuration, Spring Integration, Dependency management, and other features are available in Spring Boot framework. In this article, we will explain Spring Boot Consuming and Producing JSON. Here we will create two API endpoints for both Consuming and Producing. For Consuming purposes, we created one POST mapping API endpoint, and for producing purposes, we created a GET mapping API endpoint means This API can return the response type of MyData POJO.
In this article, we will discuss how to consume and produce JSON in the Spring Boot framework. For this, we have used Rest APIs and we have created a RestController by using @RestController annotation. After this, we created two API endpoints for handling those Consuming and Producing JSON.
Project Folder Structure:
Below we can see the project structure, after creating the file structure successfully.

Steps to Implement Spring Boot Consuming and Producing JSON
Below are the steps and implementation to demonstrate Spring Boot Consuming and Producing JSON.
Step 1:
First create a basic Spring Boot Stater project by using Spring initializr with required project dependencies.
Dependencies:
Below is the required dependencies:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
main java class:
Java
package com.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Step 2:
Once Project is successfully created, I created one POJO class that is MyData. In this class I declare name and age with lombok dependency. @AllArgsConstructor, @NoArgsConstructor and @Data these annotation are available in lombok dependency. And The @AllArgsConstructor used for creating a constructor with required arguments, @NoArgsConstructor used for creating default constructor and the final annotation @Data is used for handling Data through this pojo class.
Java
package com.app;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class MyData {
private String name;
private int age;
}
Step 3:
Now we create one more java class in main package that is MyController which is worked as RestController. For this RestController creation I use @RestController annotation. After that we created a RequestMapping that is @RequestMapping("/api"). After this we created one method that is postMethod() which takes MyData class as input by using @RequestBody finally we print that result. And this method is worked as Consuming JSON.
After this we created one more method that is MyData method which is GetMapping. For this we used @GetMapping annotation. Then we return the MyData object as response. And this method worked as Producing JSON.
Java
package com.app;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class MyController {
@PostMapping("/post")
public String postMethod(@RequestBody MyData data) {
return "Received: " + data.toString();
}
@GetMapping("/get")
public MyData getMethod() {
return new MyData("John", 30);
}
}
Step 4:
Once development is completed, Then we run this project as Spring Boot App. This project running in port number 8080 by help of the Apache Tomcat Server.
Project runningStep 5:
Now we test each Rest API with Post Man tool. First we test the post API and we provide the input in the body section you can observe in the below image. Once click on the send button The API is return a response in the form of MyData.
http://localhost:8080/api/post
output 1Step 6:
After this, we tested remaining API that is get API which takes an empty body in the body section. We can observe this in the below image. And Once click on send button, it will return a MyData object as response.
http://localhost:8080/api/get
Similar Reads
Introduction to Spring Boot Spring is widely used for creating scalable applications. For web applications, Spring provides Spring MVC, a commonly used module for building robust web applications. The major drawback of traditional Spring projects is that configuration can be time-consuming and overwhelming for new developers.
5 min read
A Guide to RestClient in Spring Boot In Spring Boot applications, external services often need to be communicated via REST APIs. Traditionally, RestTemplate was used for this purpose, but it is now considered a legacy approach. Starting from Spring Framework 6.1 and Spring Boot 3.2, RestClient has been introduced as a modern alternativ
9 min read
Spring Boot â API Documentation using springdoc-openapi For any web application, having clear API documentation is important. It helps both developers and users understand how to use the API. Like what data to send, what kind of response to expect. Thatâs where springdoc-openapi comes in. It is a Java library that automatically creates API documentation
5 min read
Storing PostgreSQL JSONB Using Spring Boot and JPA PostgreSQLâs JSONB data type provides a way to store and query JSON (JavaScript Object Notation) data in a structured format. Unlike JSON, JSONB stores the data in a binary format, enabling efficient access, indexing, and querying. This makes it ideal for handling semi-structured data in a relationa
8 min read
Spring Boot â REST API Documentation using Swagger REST stands for Representational State Transfer. REST is an architectural design pattern that defines constraints that are used in web service development. Swagger is a framework in which we can test our REST APIs for different HTTP requests i.e. :GETPOSTPUTDELETEIn this article, we will be discussi
4 min read
Spring Boot - Integrating Hibernate and JPA Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se
3 min read