What is @ResponseBody Annotation in Spring Boot?
@ResponseBody is an annotation in Spring Boot and Spring MVC used to convert the return value of a controller method directly into the HTTP response body.
Instead of returning a view page like JSP or Thymeleaf HTML templates,
@ResponseBody sends the actual data back to the client.
The response data can be:
- Plain text
- JSON
- XML
- Java objects
- Collections or lists
In simple words, @ResponseBody tells Spring Boot:
“Do not return a view page. Return the data directly to the client.”
Why is @ResponseBody Used?
In traditional Spring MVC applications, controller methods usually return view names like JSP or Thymeleaf templates.
Example:
@Controller
public class HomeController {
@GetMapping("/home")
public String home() {
return "home";
}
}
Here:
- Spring searches for
home.jsporhome.html - The view page is rendered
But modern applications and REST APIs usually return data instead of pages.
Example:
{
"name": "Naresh",
"course": "Spring Boot"
}
To return data directly, Spring Boot uses @ResponseBody.
Package of @ResponseBody
import org.springframework.web.bind.annotation.ResponseBody;
Basic Syntax of @ResponseBody
@ResponseBody
Simple Example of @ResponseBody
@Controller
public class HelloController {
@GetMapping("/hello")
@ResponseBody
public String hello() {
return "Welcome to Spring Boot";
}
}
Output
Welcome to Spring Boot
How @ResponseBody Works Internally
When a client sends an HTTP request:
- Spring DispatcherServlet receives the request
- Request is forwarded to the controller
- Controller method executes
- @ResponseBody tells Spring not to resolve a view
- Spring converts return value into HTTP response
- Data is sent back to the client
Returning JSON Using @ResponseBody
One of the most common uses of @ResponseBody is returning JSON data.
Example
class Student {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
Controller
@Controller
public class StudentController {
@GetMapping("/student")
@ResponseBody
public Student getStudent() {
return new Student(101, "Naresh");
}
}
JSON Response
{
"id": 101,
"name": "Naresh"
}
How JSON Conversion Happens
Spring Boot automatically converts Java objects into JSON using:
- Jackson library
- HTTP Message Converters
This automatic conversion is one of the biggest advantages of Spring Boot REST APIs.
Using @ResponseBody with List
@Controller
public class CourseController {
@GetMapping("/courses")
@ResponseBody
public List<String> courses() {
return Arrays.asList(
"Java",
"Spring Boot",
"Microservices"
);
}
}
Output
[
"Java",
"Spring Boot",
"Microservices"
]
Using @ResponseBody with Map
@Controller
public class EmployeeController {
@GetMapping("/employee")
@ResponseBody
public Map<String, Object> employee() {
Map<String, Object> map = new HashMap<>();
map.put("id", 1);
map.put("name", "Naresh");
map.put("role", "Developer");
return map;
}
}
Output
{
"id": 1,
"name": "Naresh",
"role": "Developer"
}
Difference Between @Controller and @ResponseBody
| Feature | @Controller | @ResponseBody |
|---|---|---|
| Main Purpose | Handles web requests | Returns response data directly |
| View Resolution | Yes | No |
| Response Type | View Page | Actual Data |
Difference Between @ResponseBody and @RestController
@RestController is a combination of:
@Controller + @ResponseBody
Example Using @ResponseBody
@Controller
public class ProductController {
@GetMapping("/product")
@ResponseBody
public String product() {
return "Laptop";
}
}
Equivalent Using @RestController
@RestController
public class ProductController {
@GetMapping("/product")
public String product() {
return "Laptop";
}
}
Which One is Better?
For REST APIs, @RestController is usually preferred because it automatically
applies @ResponseBody to all methods.
Real-Time Example in E-Commerce Application
Suppose an e-commerce website needs to return product details as JSON.
@RestController
public class ProductController {
@GetMapping("/product")
public Product getProduct() {
return new Product(
101,
"iPhone",
85000
);
}
}
JSON Response
{
"id": 101,
"name": "iPhone",
"price": 85000
}
Advantages of @ResponseBody
- Easy REST API development
- Automatic JSON conversion
- Reduces boilerplate code
- Supports multiple response formats
- Improves frontend-backend communication
- Works well with AJAX and mobile applications
- Supports modern microservices architecture
Disadvantages of @ResponseBody
- Not suitable for returning view pages
- Large responses may affect performance
- Sensitive data exposure risk if DTOs are not used properly
Common Errors with @ResponseBody
1. Missing Jackson Dependency
If Jackson dependency is missing, JSON conversion may fail.
Spring Boot Starter Web usually includes Jackson automatically.
2. No Getter Methods
If Java objects do not contain getter methods, JSON serialization may not work correctly.
3. Circular Reference Error
Bidirectional relationships in JPA entities may create infinite JSON recursion.
Solution:
- Use DTOs
- Use
@JsonIgnore - Use
@JsonManagedReference
Best Practices for Using @ResponseBody
- Use @RestController for REST APIs
- Use DTO classes instead of entities directly
- Handle exceptions properly
- Validate response data
- Do not expose sensitive information
- Use proper HTTP status codes
Common Interview Questions on @ResponseBody
What is @ResponseBody in Spring Boot?
@ResponseBody is an annotation used to return data directly as an HTTP response instead of returning a view page.
What is the difference between @Controller and @RestController?
@RestController combines @Controller and @ResponseBody, making it ideal for REST API development.
How does Spring Boot convert Java objects into JSON?
Spring Boot uses Jackson and HTTP Message Converters to convert Java objects into JSON.
Can @ResponseBody return JSON?
Yes. It can return JSON, XML, strings, lists, maps, and Java objects.
Is @ResponseBody required with @RestController?
No. @RestController already includes @ResponseBody internally.
Difference Between Traditional MVC and REST API
| Feature | Traditional MVC | REST API |
|---|---|---|
| Returns | HTML Views | JSON/XML Data |
| Main Annotation | @Controller | @RestController |
| Frontend | Server-side rendering | Separate frontend/mobile apps |
Conclusion
@ResponseBody is one of the most important annotations in Spring Boot and Spring MVC. It allows developers to return data directly to clients instead of rendering view pages.
It plays a major role in REST API development, microservices architecture, frontend-backend communication, and modern web applications.
Spring Boot automatically converts Java objects into JSON using Jackson, making API development simple and efficient.
Understanding @ResponseBody is essential for Spring Boot developers because it is widely used in enterprise applications, RESTful web services, cloud-native systems, and technical interviews.