What is the difference between @Controller and @RestController in Spring?
Learn the difference between @Controller and @RestController in Spring: view rendering vs JSON serialization, when to use each, with code examples.
Expected Interview Answer
@Controller returns view names that are resolved into HTML pages, while @RestController is a convenience annotation that combines @Controller and @ResponseBody so every handler method's return value is serialized directly into the HTTP response body (typically JSON).
With @Controller you usually pair methods with a view technology (Thymeleaf, JSP) and add @ResponseBody manually when you want to return data instead of a view. @RestController removes that boilerplate — introduced in Spring 4.0, it applies @ResponseBody to every method automatically, making it the standard choice for REST APIs that return JSON or XML. Use @Controller for server-rendered web pages and @RestController for data-serving endpoints.
- @RestController removes repetitive @ResponseBody annotations
- Clear intent: web pages vs data APIs
- Automatic serialization to JSON/XML
- @Controller supports server-side view rendering
- Cleaner, more concise REST controllers
AI Mentor Explanation
@Controller is like a scoreboard operator who assembles the full painted display for the crowd to view, while @RestController is the data feed streaming raw ball-by-ball numbers to a broadcaster's app. One hands over a finished visual page; the other hands over pure data for another system to consume.
Step-by-Step Explanation
Step 1
Understand @Controller
It marks an MVC controller whose methods return view names resolved into HTML by a view resolver.
Step 2
Add @ResponseBody when needed
On a plain @Controller, annotate methods with @ResponseBody to return data instead of a view.
Step 3
Meet @RestController
It is @Controller + @ResponseBody combined, applied to every handler method automatically.
Step 4
Return data directly
With @RestController, return objects and Spring serializes them to JSON/XML via HttpMessageConverters.
Step 5
Choose per use case
Use @Controller for server-rendered pages and @RestController for REST/JSON APIs.
What Interviewer Expects
- @RestController equals @Controller plus @ResponseBody
- How each affects the HTTP response (view vs serialized body)
- When to use each in a real application
- Awareness of HttpMessageConverters serializing return values
- That @RestController was added in Spring 4.0
Common Mistakes
- Thinking @Controller cannot ever return JSON
- Not knowing @RestController implies @ResponseBody
- Using @RestController for server-rendered HTML pages
- Forgetting to add @ResponseBody on a plain @Controller returning data
- Confusing the roles of view resolvers and message converters
Best Answer (HR Friendly)
“@Controller is used when your app builds full web pages to show users, while @RestController is used to build APIs that return raw data like JSON. @RestController is basically @Controller with an automatic setting that sends data straight back instead of rendering a page.”
Code Example
@Controller
public class PageController {
@GetMapping("/home")
public String home() {
return "home"; // resolves to home.html view
}
}
@RestController
public class ApiController {
@GetMapping("/api/user")
public User user() {
return new User(1, "Ada"); // serialized to JSON automatically
}
}Follow-up Questions
- What does @ResponseBody do on a controller method?
- How does Spring convert a returned object into JSON?
- Can a single @Controller mix view methods and @ResponseBody methods?
- What is a view resolver and when is it used?
- When would you still choose @Controller over @RestController?
MCQ Practice
1. @RestController is equivalent to which combination?
@RestController is a convenience annotation combining @Controller and @ResponseBody.
2. A method in a plain @Controller returns the string "home". What happens by default?
Without @ResponseBody, the returned string is interpreted as a view name by the view resolver.
3. Which annotation is the standard choice for a JSON REST API?
@RestController auto-applies @ResponseBody, serializing return values to JSON for API endpoints.
Flash Cards
What is @RestController? — A convenience annotation combining @Controller and @ResponseBody, serializing return values to the response body.
What does a plain @Controller return by default? — A view name that a view resolver turns into an HTML page.
How do you make a @Controller method return JSON? — Add @ResponseBody to the method (or the class).
When use @Controller vs @RestController? — @Controller for server-rendered pages; @RestController for REST/JSON APIs.
Continue Learning
Related Interview Questions
How does exception handling work in Spring MVC with @ControllerAdvice?
medium
What is the DispatcherServlet and how does the Spring MVC request flow work?
medium
What is the difference between @Component, @Service, @Repository, and @Controller in Spring?
easy
How does Spring Boot embed a web server and how does the embedded Tomcat work?
medium