@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.
Key Points: • @Controller is mainly used for MVC-based web applications. • @RestController is used for building RESTful web services and APIs. • @RestController is a combination of @Controller and @ResponseBody. • Methods in @Controller usually return view names. • Methods in @RestController return response data directly.
How @Controller Works
When a method returns a String, Spring treats it as a view name and forwards the request to a view resolver.
Code Example:
@Controller
public class HomeController {
@GetMapping("/home")
public String home() {
return "home";
}
}Flow:
Client Request | @Controller | Returns View Name | View Resolver | home.jsp / home.html
Output:
A web page is rendered and sent to the browser.
How @RestController Works
@RestController automatically adds @ResponseBody behavior to every method.
Code Example:
@RestController
public class EmployeeController {
@GetMapping("/employee")
public Employee getEmployee() {
return new Employee(
101,
"John");
}
}Flow:
Client Request | @RestController | Java Object | JSON Conversion | JSON Response
Output:
{ "id": 101, "name": "John" }
Internal Difference
@Controller
Equivalent To:
@Controller
Methods return:
• View names • ModelAndView objects • Views
@RestController
Equivalent To:
@Controller
@ResponseBodyMethods return:
• JSON • XML • Plain text • ResponseEntity
Comparison
@ControllerPurpose: • Traditional MVC applications
Response: • View/Page
Requires @ResponseBody: • Yes, for JSON responses
Common Usage: • JSP, Thymeleaf, HTML pages
@RestController
Purpose: • REST APIs
Response: • JSON/XML/Data
Requires @ResponseBody: • No
Common Usage: • Microservices and REST services
Returning JSON from @Controller
If JSON is required in a @Controller, use @ResponseBody.
Code Example:
@Controller
public class EmployeeController {
@GetMapping("/employee")
@ResponseBody
public Employee getEmployee() {
return new Employee(
101,
"John");
}
}This behaves similarly to @RestController for that specific method.
Example:
Online Banking Application
Web Portal:
@Controller
Used for:
• Login page • Dashboard page • Transaction history page
REST API:
@RestController
Used for:
• Account APIs • Fund transfer APIs • Mobile application APIs
Benefits of @RestController
• Less boilerplate code • Ideal for REST APIs • Automatic JSON conversion • Better suited for microservices • Cleaner and simpler code
When to Use Which?
Use @Controller when:
• Returning HTML pages • Using JSP or Thymeleaf • Building MVC applications
Use @RestController when:
• Building REST APIs • Returning JSON/XML responses • Developing microservices
Interview Tip: 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."