@ResponseBody in Spring MVC — Java Object to JSON Serialization
Fast recall
Quick revision
Core answer
Understanding of serialization direction and @ResponseBody's role
Adding @ResponseBody explicitly to @RestController methods — it's already included
Explain that @RestController = @Controller + @ResponseBody as a meta-annotation
Interview-ready guide
Interview answer
Returning an OrderDto from a response-body method commonly produces JSON because the web starter supplies Jackson support. Returning byte[] or Resource can use a different converter. A produces condition and Accept header can narrow the response media type. If no converter can write the chosen type, the annotation cannot make serialization succeed on its own.
@ResponseBody in Spring MVC — Java Object to JSON Serialization
Direct Answer
@ResponseBody tells Spring MVC to write a handler return value to the HTTP response through an HttpMessageConverter instead of resolving it as a view name. JSON with Jackson is one possible representation, not the definition. @RestController applies @ResponseBody semantics to its methods automatically.
Code Example
@Controller
@RequestMapping("/api/products")
public class ProductController {
@GetMapping("/{id}")
@ResponseBody // explicitly needed because this is @Controller, not @RestController
public Product getProduct(@PathVariable Long id) {
return productService.findById(id);
// Spring serializes Product object to JSON automatically
}
}
// Better: use @RestController so @ResponseBody is implicit
@RestController
@RequestMapping("/api/orders")
public class OrderController {
@GetMapping
public List<Order> getAllOrders() {
return orderService.findAll(); // no @ResponseBody needed, it's implicit
}
}
record Product(Long id, String name, double price) {}
record Order(Long id, String customer, List<String> items) {}
Common Mistakes & Pitfalls
- Adding @ResponseBody to @RestController methods: Freshers see @ResponseBody in tutorials and add it explicitly even when using @RestController. It's redundant — @RestController already includes @ResponseBody on every method.
- Returning String thinking it will be JSON: If you return a plain String like "success" from a @ResponseBody method, Spring sends exactly that string (not JSON-encoded). Freshers expect {"message":"success"} but get the raw text. Return an object or record instead.
- Mixing view names with @ResponseBody: In a @Controller with @ResponseBody on some methods, freshers forget which mode they're in. A method without @ResponseBody tries to find a template named after the returned String, leading to template not found errors. Keep REST endpoints in @RestController classes separate from view controllers.
Check your understanding
Answer these without looking back at the article.
- What is the difference between returning a String from @Controller and @RestController?
- What happens when a @RestController method returns null?
- How do you return both JSON and HTML from the same controller class?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.