Spring Annotations for REST Services
Here over this article, we are discussing different REST specific annotations in Spring.
@Controller
We can annotate classic controllers with the @Controller
annotation. This is simply a specialization of the @Component
class, which allows us to auto-detect implementation classes through classpath scanning.
We typically use @Controller
it in combination with an @RequestMapping
annotation for request handling methods.
@RestController
@RestController
is a specialized version of the controller. It includes the @Controller
and @ResponseBody
annotations, and as a result, simplifies the controller implementation.
What is the difference between @Controller and @RestController?
The @Controller is a common annotation that is used to mark a class as Spring MVC Controller while @RestController is a special controller used in RESTFul web services and the equivalent of @Controller + @ResponseBody.
The @RestController
is relatively new annotation,
The @Controller
annotation indicates that the class is a “Controller” like a web controller while @RestController
annotation indicates that the class is a controller where @RequestMapping
…