Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

Spring Boot REST API - Returning Response in JSON Format

by Ayush Shrivastava

.

Updated Thu Aug 21 2025

.
Spring Boot REST API - Returning Response in JSON Format

Spring Boot REST API - Returning Response in JSON Format

Mastering (10) (1).pngIntroduction

In this guide, we will learn how to return a response in JSON format from a Spring Boot REST API. By default, Spring Boot returns responses in JSON, making it the standard format for most RESTful services. JSON is widely used due to its lightweight nature, easy readability, and compatibility with various programming languages.

Why Use JSON in REST APIs?

  • Lightweight & Fast: JSON data is compact, making it faster to parse compared to XML.

  • Human & Machine Readable: JSON has a simple and structured format that is easy to read and manipulate.

  • Widely Supported: JSON is supported across multiple programming languages and frameworks.

  • Ideal for Web & Mobile Applications: JSON works well with JavaScript and is natively supported in web and mobile development.

Steps to Implement JSON Response in Spring Boot

1. Add Required Dependencies

Spring Boot uses Jackson for JSON serialization and deserialization. Jackson is included by default in Spring Boot starters, so no extra dependency is needed. However, ensure that your pom.xml contains the necessary dependency:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>

This ensures that Jackson can automatically convert Java objects to JSON and vice versa.

2. Create a Model Class (DTO) for JSON Response

We need a model class (DTO) that represents the response data. Jackson automatically serializes objects into JSON.

package com.masteringbackend.demo.dto;

public class UserResponse {
    private String name;
    private int age;
    private String email;

    // Default Constructor
    public UserResponse() {}

    // Parameterized Constructor
    public UserResponse(String name, int age, String email) {
        this.name = name;
        this.age = age;
        this.email = email;
    }

    // Getters and Setters
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }

    public String getEmail() { return email; }
    public void setEmail(String email) { this.email = email; }
}

3. Create a REST Controller

Next, we create a Spring Boot REST Controller that returns the response in JSON format. We use produces = MediaType.APPLICATION_JSON_VALUE to specify JSON response type.

package com.masteringbackend.demo.controller;

import com.example.demo.dto.UserResponse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class UserController {

    @GetMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE)
    public UserResponse getUser() {
        return new UserResponse("Ayush", 25, "[email protected]");
    }
}

4. Create Main Spring Boot Application Class

The main class is needed to start the Spring Boot application.

package com.masteringbackend.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

5. Run the Application and Test the API

Start the Spring Boot application, then send a GET request using Postman, Browser, or Curl:

GET <http://localhost:8080/api/user>

6. Expected JSON Response Output

When the API is called, it returns the response in JSON format as shown below:

{
    "name": "Ayush",
    "age": 25,
    "email": "[email protected]"
}

Troubleshooting Common Issues

  1. Error: JSON Response Not Showing?

  • Ensure that the jackson-databind dependency is added.

  • Check if produces = MediaType.APPLICATION_JSON_VALUE is correctly set in the controller.

  • Use Postman and check the Accept header; set it to application/json.

  1. JSON Response Showing as XML?

  • If you have jackson-dataformat-xml dependency, Spring Boot might return XML.

  • Remove any @XmlRootElement annotations from the model class.

  • Explicitly request JSON by setting the Accept header to application/json.

Conclusion

Spring Boot provides an easy way to return JSON responses with minimal configuration. With proper setup, JSON-based REST APIs are efficient, fast, and easy to integrate with modern web and mobile applications.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board

Backend Tips, Every week

Backend Tips, Every week