@RestController in Spring Boot — Annotation Explained
Fast recall
Quick revision
Core answer
Understanding of Spring's most common REST annotation and how it simplifies JSON API development compared to traditional @Controller.
Adding @ResponseBody to methods inside a @RestController class, which is redundant since @RestController already implies @ResponseBody for all methods.
Explain that @RestController is a meta-annotation (composed annotation) that internally combines @Controller and @ResponseBody, showing knowledge of Spring's annotation composition model.
Interview-ready guide
Interview answer
When a method returns CustomerDto and JSON is negotiated, the Jackson message converter normally writes it as JSON. If it returns Resource for a file download, a resource converter writes the body. A String can become text. @RestController removes repeated @ResponseBody annotations; it does not mean every response has Content-Type application/json.
@RestController in Spring Boot — Annotation Explained
Direct Answer
@RestController combines @Controller and @ResponseBody. It registers an MVC controller whose handler return values use response-body processing rather than view-name resolution. A compatible HttpMessageConverter writes the response; JSON via Jackson is common, but the annotation also supports text, XML, bytes, resources, and other configured representations.
Code Example
@RestController
@RequestMapping("/api/users")
public class UserController {
private final UserRepository userRepository;
public UserController(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping("/{id}")
public User getUser(@PathVariable Long id) {
return userRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("User not found")); // Jackson serializes User to JSON
}
@PostMapping
public User createUser(@RequestBody User user) { // @RequestBody deserializes JSON to User
return userRepository.save(user); // Return value becomes JSON automatically
}
}
Common Mistakes & Pitfalls
- Redundant @ResponseBody: Freshers often add @ResponseBody to individual methods inside a @RestController class. This is unnecessary because @RestController already applies @ResponseBody to all methods. It's like wearing two jackets—the annotation is already there.
- Mixing @Controller and @RestController: Using @Controller for REST endpoints and manually adding @ResponseBody everywhere. This creates inconsistency in the codebase. Stick to @RestController for REST APIs to make intent clear.
- Forgetting @RequestMapping: Placing @RestController without a base @RequestMapping path means every method needs a full path, leading to duplication. Always define the base path at class level for cleaner code.
Check your understanding
Answer these without looking back at the article.
- What is the difference between @Controller and @RestController?
- What HTTP status code does @RestController return by default?
- How do you return a custom HTTP status code from a @RestController method?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.