Junior
What is the difference between @RestController and @Controller annotations in Spring?
sobes.tech AI
Answer from AI
@Controller is used to define web controller classes that handle requests and return a view (e.g., JSP, Thymeleaf) for display. The return value of the method is usually a logical view name.
@RestController is a specialized annotation that combines the functionality of @Controller and @ResponseBody. It indicates that the controller directly returns data (objects, JSON, XML, etc.) in the response body, rather than a view name. This is convenient for creating RESTful web services.
The key difference in behavior when returning a value:
| Annotation | Return behavior | Application |
|---|---|---|
| @Controller | View name | Traditional web applications |
| @RestController | Data (response body) | RESTful web services |