@Controller and @RestController are Spring MVC annotations used to handle incoming HTTP requests. The primary difference is that @Controller is typically used for web applications that return views (HTML/JSP pages), while @RestController is designed for REST APIs and returns data directly in formats such as JSON or XML.
How @Controller Works
When a method returns a String, Spring treats it as a view name and forwards the request to a view resolver.
@Controller
public class HomeController {
@GetMapping("/home")
public String home() {
return "home";
}
}Client Request
|
@Controller
|
Returns View Name
|
View Resolver
|
home.jsp / home.html
A web page is rendered and sent to the browser.
How @RestController Works
@RestController automatically adds @ResponseBody behavior to every method.
@RestController
public class EmployeeController {
@GetMapping("/employee")
public Employee getEmployee() {
return new Employee(
101,
"John");
}
}Client Request
|
@RestController
|
Java Object
|
JSON Conversion
|
JSON Response
{
"id": 101,
name": "John
}
Internal Difference
@Controller
@Controller
@RestController
@Controller
@ResponseBodyComparison
@Controller@RestController
Returning JSON from @Controller
If JSON is required in a @Controller, use @ResponseBody.
@Controller
public class EmployeeController {
@GetMapping("/employee")
@ResponseBody
public Employee getEmployee() {
return new Employee(
101,
"John");
}
}This behaves similarly to @RestController for that specific method.
Online Banking Application
@Controller
@RestController
Benefits of @RestController
When to Use Which?
A concise interview answer is
@Controller is used in Spring MVC applications to return views such as JSP or Thymeleaf pages. @RestController is a specialized version of @Controller that combines @Controller and @ResponseBody, causing all methods to return data directly as JSON or XML. Therefore, @Controller is typically used for web pages, while @RestController is preferred for REST APIs and microservices.
Last updated: 11 November 2026
Previous question: What does the @RestController annotation do in Spring Boot?
Next question: What is the difference between @RequestMapping and @GetMapping?