Spring Framework provides two commonly used annotations for handling web requests: @Controller and @RestController. They may look similar, but they are designed for different purposes:
- @Controller is typically used for MVC-based web applications (returning HTML views).
- @RestController is used for RESTful APIs (returning JSON/XML responses)
@Controller Annotation
- @Controller is a Spring annotation used to define a web controller in an MVC (Model-View-Controller) application.
- It usually works with view resolvers (like Thymeleaf, JSP or FreeMarker) to return HTML pages.
- If you want to return JSON/XML data, you must use @ResponseBody with each method.
Example: Using @Controller in a Spring MVC Application
@Controller
public class HomeController {
@GetMapping("/home")
public String homePage(Model model) {
model.addAttribute("message", "Welcome to Spring MVC!");
// Returns a view (home.html or home.jsp)
return "home";
}
}
Explanation:
- The @Controller annotation marks the class as a Spring MVC controller.
- The homePage() method maps to /home and returns a view named home.
@RestController Annotation
- The @RestController annotation is used for making restful web services.
- This annotation is used at the class level and allows the class to handle the requests made by the client.
- The RestController allows to handle all REST APIs such as GET, POST, Delete and PUT requests.
- It is equivalent to-> @Controller + @ResponseBody
Example: Using @RestController in a REST API
@RestController
public class ProductController {
@GetMapping("/product")
public Product getProduct() {
return new Product(1, "Laptop", 80000);
}
}
Explanation:
- The @RestController annotation combines @Controller and @ResponseBody.
- The getProduct() method returns a Product object that is automatically converted into JSON.
@Controller vs @RestController
The table below demonstrates the differences between @Controller and @RestController annotations in Spring.
Feature | @Controller | @RestController |
|---|---|---|
Definition | This annotation is used to mark classes as Spring MVC controllers. | This is a specialized controller used for RESTful web services. |
Inheritance | Extends @Component annotation. | Extends @Controller annotation. |
View Support | Returns a view in Spring Web MVC. | Cannot return a view. |
Response Handling | Requires @ResponseBody for sending responses as JSON or XML. | Assumes @ResponseBody by default, eliminating the need for explicit annotations. |
Request Handling | Used for traditional MVC applications where views (JSP, Thymeleaf) are returned. | Used for REST APIs where JSON or XML responses are sent directly to the client. |
Spring Version | Introduced in Spring 2.5. | Introduced in Spring 4.0. |
When to Use Which
Use @Controller when:
- You are building a web application that returns HTML pages.
- You want to integrate with frontend template engines like Thymeleaf or JSP.
Use @RestController when:
- You are building REST APIs for mobile apps, SPAs or microservices.
- You need to exchange data in JSON/XML format.