Spring Annotations for REST Services

Anish Antony
Javarevisited
Published in
3 min readJun 19, 2021

--

Here over this article, we are discussing different REST specific annotations in Spring.

Photo by Jess Bailey on Unsplash

@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

--

--

Anish Antony
Javarevisited

Fullstack Developer | Blogger | Experience on Java, Python, React, Angular, Golang | http://www.behindjava.com