Difference Between @Controller and @RestController Annotation in Spring


Spring Annotations can be referred to as metadata which provides information related to a program. Supplemental information of a program can be derived from annotations. The annotation will not affect the program nor the action of the compiled program be changed. Spring annotations are of two types which are @Controller and @RestController. In this article, we will see the difference between @Controller and @RestController.

What is @Control Annotation?

Spring @Controller Annotation is responsible for informing that a particular class can behave in the form of a controller. The @Controller annotation and the annotated handler methods are used in combination with each other on the basis of @RequestMapping Annotation. The @Controller annotation can be used with classes only and not with any other entity. This annotation can be used successfully with the Spring MVC Applications.

What is @RestController Annotation?

RestController is used for the development of restful web services by using @RestController Annotation. This annotation can be used at a class level and it is responsible to give permission to take care of the requests that clients have made. It has the ability to handle the following requests −

  • GET
  • POST
  • Delete
  • PUT

Difference between @Controller and @RestController

The difference between @Controller and @RestController which can be found in the table below −

@Controller @RestController
@Controller can work as Spring MVC Controller. @RestController is used in RESTful Web Services. It works by combining @Controller annotation with @ResponseBody annotation.
@Controller Annotation is one of the special versions of the @Component annotation. @RestController is the specialized version of the @Controller annotation.
A view in the Spring MVC can be returned by @Controller annotation. No views can be returned by the @RestController Annotation.
@Controller annotation can be used with a class and it indicates that the class is a controller. @RestController is also used with a class and indicates that the class is a controller. @RequestMapping and @ResponseBody are used in these classes by default.
@ResponseBody method has to be used as a handler method. @ResponseBody Method is not used on every handler method.
@Controller was launched in the Spring 2.5 version. @RestController was launched in the Spring 4.0 version.
@Controller is used to define a controller component in MVC. @RestController can be self-annotated with @Controller and @ResponseBody.
@Controller is used to return a view for the web applications. @RestController is used for RESTful Web Services and the data is returned in the format of JSON or XML.
View resolution is supported by @Controller. This helps in returning a view which ViewResolver can resolve. View resolution is not supported.
@Controller has the ability to return any type of content but explicit annotation is required so that data can be returned in XML or JSON formats. It produces only JSON and XML responses.
In order to include relevant headers, @ResponseBody has to be included explicitly. It includes the headers automatically and indicates the type of response it has to deliver.

Difference between Snippets for @Controller and @RestController

Here is the difference of the code snippet.

@Controller

@Controller
@ResponseBody
Public class MyController {
   Code snippet
}

@RestController

@RestController
Public class MyRestController {
   Code snippet
}

Conclusion

@Controller and @RestController are the annotations for Spring. @Controller is used for Spring MVC-based applications. @RestController provides the output in the form of JSON or XML and is used for RESTful APIs. Both the annotations have distinct features which separate them from each other.

FAQs on @Controller Vs. @RestController

1. Can @Controller annotation be used to build a REST API?

Yes! @Controller can be used to make a REST API. In this case, a response body has to be returned by each method and it should be annotated by @ResponseBody. If @ResponseBody will not be used, the return value of the method will be interpreted by Spring MVC in the form of a view name but no data will be written in the body of the response.

2. Which is better @Controller or @RestController?

@RestController seems to be better as it provides the data in JSON or XML format. The development of RESTful APIs also becomes simple by using @RestController. @RestController also has the ability to use the @ResponseBody automatically.

3. Can @RestController replace @Controller?

@RestController can replace @Controller if a RESTful service is to be created. In other cases, both the annotations have distinct features. @Controller is used for Spring MVC-based applications.

4. Can @Component be used as a replacement for @Controller?

If web-specific functionalities are to be used, @Controller is the better option as this annotation has been developed for web layer components. @Controller has the ability to handle HTTP requests easily.

5. What is the main difference between @Controller and @RestController?

@RestController uses the @ResponseBody automatically but this is not the case with the @Controller annotation.

Updated on: 22-Jul-2024

0 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements