Difference Between @Controller and @RestController Annotation in Spring
This article is a summary of using @Controller and @RestController annotations in the Spring framework and their differences.
Let’s get started with the definition of the @Controller and @RestController annotations.
There is a screenshot below which shows the package hierarchy and the use of @Controller annotation in the controller class.
@Controller: This annotation indicates that a particular class serves the role of a controller
@RequestMapping: To map to the Spring MVC controller method, use this.
@ResponseBody: This is used to bind the domain object in the return type with the HTTP response body.
The @RestController annotation is used to create restful web services. @RestController is a convenience annotation that does add the @Controller and @ResponseBody annotations.
You can see the comparison table below;
As a result, the main differences are mentioned in the post. You can use the following references for further reading.
References:
https://www.geeksforgeeks.org/spring-controller-annotation-with-example/
https://www.geeksforgeeks.org/spring-rest-controller/
https://www.geeksforgeeks.org/difference-between-controller-and-restcontroller-annotation-in-spring/