What are the differences between the annotations @Controller and @RestController?
- Annotations are a key feature of the Spring framework and are used to mark classes and methods to provide metadata to the Spring container.
- We use @ResponseBody in all handling methods in @Controller to indicate that the method returns the response in the body of the response object. On the other hand, in @RestController, we don’t need to use @ResponseBody on every handler method since @RestController combines @Controller and @ResponseBody.
- @Controller is used for traditional Spring MVC controllers that return views, while @RestController is used for RESTful web service controllers that return data directly.
- @Controller first appeared in Spring 2.5 version whereas @RestController started in Spring 4.0 version.
BY Best Interview Question ON 07 Apr 2023