How to Return JSON Response in Spring Boot Easily
In Spring Boot, you return a JSON response by creating a method in a
@RestController that returns an object. Spring Boot automatically converts the object to JSON using Jackson. You can also use ResponseEntity to customize the response status and headers.Syntax
Use @RestController on your class to tell Spring Boot this controller returns data, not views. Define a method with @GetMapping or other HTTP method annotations. Return a Java object or ResponseEntity<T>. Spring Boot converts the object to JSON automatically.
@RestController: Marks the class as a REST controller.@GetMapping("/path"): Maps HTTP GET requests to the method.- Return type: Java object or
ResponseEntitywrapping the object. - Spring Boot uses Jackson to convert the object to JSON.
java
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.http.ResponseEntity; @RestController public class MyController { @GetMapping("/example") public MyData getData() { return new MyData("Hello", 123); } @GetMapping("/custom-response") public ResponseEntity<MyData> getCustomResponse() { MyData data = new MyData("World", 456); return ResponseEntity.ok().body(data); } static class MyData { private String message; private int number; public MyData(String message, int number) { this.message = message; this.number = number; } public String getMessage() { return message; } public int getNumber() { return number; } } }
Example
This example shows a Spring Boot REST controller with two endpoints. The first returns a simple Java object that Spring Boot converts to JSON. The second returns a ResponseEntity to customize the HTTP response.
java
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.http.ResponseEntity; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } @RestController class MyController { @GetMapping("/hello") public Greeting sayHello() { return new Greeting("Hello, Spring Boot!"); } @GetMapping("/custom") public ResponseEntity<Greeting> customGreeting() { Greeting greeting = new Greeting("Custom Response"); return ResponseEntity.ok().header("X-Custom-Header", "value").body(greeting); } static class Greeting { private String message; public Greeting(String message) { this.message = message; } public String getMessage() { return message; } } }
Output
{
"message": "Hello, Spring Boot!"
}
// For /custom endpoint, JSON body is same but with custom header in HTTP response
Common Pitfalls
- Not using
@RestControllerbut@Controllercauses Spring to look for a view instead of returning JSON. - Returning
Stringdirectly returns plain text, not JSON. - Missing getters in your data class prevents JSON serialization.
- For complex responses, forgetting to use
ResponseEntitylimits control over HTTP status and headers.
java
/* Wrong: Using @Controller returns view name, not JSON */ import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class WrongController { @GetMapping("/wrong") public String getString() { return "Hello"; // returns view named 'Hello', not JSON } } /* Right: Use @RestController to return JSON */ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.GetMapping; @RestController public class RightController { @GetMapping("/right") public String getString() { return "Hello"; // returns JSON string "Hello" } }
Quick Reference
Remember these key points when returning JSON in Spring Boot:
- Use
@RestControllerfor REST APIs. - Return Java objects; Spring Boot converts them to JSON.
- Use
ResponseEntityto customize status and headers. - Ensure your data classes have public getters.
- Jackson is the default JSON converter.
Key Takeaways
Use @RestController to automatically return JSON responses in Spring Boot.
Return Java objects from controller methods; Spring Boot converts them to JSON with Jackson.
Use ResponseEntity to customize HTTP status codes and headers in your JSON responses.
Ensure your data classes have public getters for proper JSON serialization.
Avoid using @Controller alone when you want to return JSON data.