Open In App

Difference Between @Controller and @RestController Annotation in Spring

Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. 

@Controller Annotation: Spring @Controller annotation is also a specialization of @Component annotation. The @Controller annotation indicates that a particular class serves the role of a controller. Spring Controller annotation is typically used in combination with annotated handler methods based on the @RequestMapping annotation. It can be applied to classes only. It’s used to mark a class as a web request handler. It’s mostly used with Spring MVC applications. This annotation acts as a stereotype for the annotated class, indicating its role. The dispatcher scans such annotated classes for mapped methods and detects @RequestMapping annotations.



Tip: Do read more about Spring @Controller Annotation with Example to get a better understanding. 

@RestController Annotation: RestController is used for making restful web services with the help of the @RestController annotation. This annotation is used at the class level and allows the class to handle the requests made by the client. Let’s understand @RestController annotation using an example. The RestController allows to handle all REST APIs such as GET, POST, Delete, and PUT requests. 



Tip: Do read more about Spring – REST Controller to get a better understanding. 

Let us now come up with a big major difference between the two which is as follows. Here @Controller is used to mark classes as Spring MVC Controller whereas @RestController is a convenience annotation that does nothing more than adding the @Controller and @ResponseBody annotations for which refer to the below code snippet as follows:  

@Controller
@ResponseBody

class Controller
{
    ------
    ------
    ------
}

It is equivalent to

@RestController

class RestController
{
    ------
    ------
    ------
}

Let us finally conclude the differences between them via tabular format which is depicted below in a tabular format as follows:  

@Controller

@RestController

@Controller is used to mark classes as Spring MVC Controller. @RestController annotation is a special controller used in RESTful Web services, and it’s the combination of @Controller and @ResponseBody annotation.
It is a specialized version of @Component annotation. It is a specialized version of @Controller annotation.
In @Controller, we can return a view in Spring Web MVC. In @RestController, we can not return a view.
@Controller annotation indicates that the class is a “controller” like a web controller. @RestController annotation indicates that class is a controller where @RequestMapping methods assume @ResponseBody semantics by default.
In @Controller, we need to use @ResponseBody on every handler method. In @RestController, we don’t need to use @ResponseBody on every handler method.
It was added to Spring 2.5 version. It was added to Spring 4.0 version.
Article Tags :