Table of contents
1.
Introduction
2.
What is @Controller Annotation?
3.
What is @RestController Annotation?
4.
Difference between @Controller and @RestController Annotation
5.
Frequently Asked Question
5.1.
Can we replace @RestController with @Controller?
5.2.
Can we use @Component instead of @Controller?
5.3.
What are the key differences between Controller and RestController Annotations?
5.4.
Can we use Controller and RestController together?
5.5.
What is the difference between @component and @RestController?
5.6.
Why do we use @RestController in Spring Boot?
6.
Conclusion 
Last Updated: Jun 21, 2024
Easy

Difference between @Controller and @RestController

Author Rinki Deka
0 upvote

Introduction

If you are a web creator and build web applications, you might have used Spring Framework. Then you have come across the terms like Controller and RestController annotations.

The interviewer often offers these terms to examine the candidate's knowledge of Spring Boot. So it is imperative to know the definition of Controller and RestController Annotations, their use cases, and the difference between @Controller and @Restcontroller. So, In this article, we will discuss all aspects related to this topic.

Difference between @Controller and @RestController

The @Controller Annotation has always been a component of the Spring framework. As its name indicates, it defines a controller, an entrance point for a Spring Web application.

Spring version 4 has introduced RestController. @RestController annotation combines both Controller and ResponseBody annotations. 

What is @Controller Annotation?

Controller Annotation is a specialization of stereotype component annotation. It prepares a roadmap with data that needs to be displayed by the view. It indicates a class is a function of the Controller.

Suppose you were using the previous version of Spring 4 (like Spring 3 or 3.1); Then, at that time, we had to combine Controller and ResponseBody to create a RESTful response, which could have been more convenient to the programmers. So the developer created RestController. 

It is applied to classes only. It uses Spring MVC applications. The response that is released is in the form of JSON or XML and returns it to a client that has requested it. Controller is mainly combined with RequestMapping Annotation used in request handling methods.

@Controller
@RequestMapping 
Public class UserController {
  // your expressions 
}

What is @RestController Annotation?

Version Spring 4.0 have introduced RestController Annotation. A specialized version of Controller Annotation. It is a combination of behaviours of a Controller and ResponseBody. When we use this Annotation, the response is JSON or XML. Because of this, the Controller is not in charge of providing any viewpoint to the user. 

Using this Annotation, we do not have to add a response body to all requesting mapping methods. It was better and more convenient to use one Annotation than two annotations. It is nothing but a shortcut to use both Controller and ResponseBody. It gives a clear and readable response.

@RestController
@RequestMapping 
Public class UserController {
  // your expressions 
}

Difference between @Controller and @RestController Annotation

Controller Annotation RestController Annotation
It was added to the Spring 2.5 version.It was added to the Spring 4.0 version.
It is a specialized version of stereotype @Component annotation and uses Spring MVC controller.It is a combination of @Controller and @ResponseBody.
This annotation indicates that a class is a “controller” like a web controller.This annotation indicates that a “controller” where @ResponseBody is by default.
While using this we need to add @ResponseBody on every method.While using this we don't need to add @ResponseBody on every method. 
It returns an object or render a view.It returns a domain object.

Frequently Asked Question

Can we replace @RestController with @Controller?

The @RestController and @Controller annotations serve distinct purposes in Spring Framework. While @Controller is used for MVC-based web applications, @RestController combines @Controller and @ResponseBody, specifically designed for RESTful APIs. So, if we are not creating a RESTful web service, then can use @Controller.

Can we use @Component instead of @Controller?

Using @Component instead of @Controller is not suitable for web-specific functionalities like handling HTTP requests. While both are generic stereotypes annotations, @Controller is made for web layer components and supports additional features for web-related processing.

What are the key differences between Controller and RestController Annotations?

The main difference between Controller and RestController Annotation is ResponseBody Annotation. Controller annotation does not call @ResponseBody to all Controller's method itself. Whereas @RestController combines Controller and @ResponseBody, which use @ResponseBody to all its methods by default.

Can we use Controller and RestController together?

Yes, we can use both @Controller and @RestController together. Some methods can use @Controllers to return views, whereas others can use @RestController to return responses.

What is the difference between @component and @RestController?

@Component is a generic stereotype annotation in Spring, used to denote any Spring-managed component. @RestController is a specialized annotation combining @Controller and @ResponseBody, specifically for RESTful web services, automatically serializing return values into JSON or XML.

Why do we use @RestController in Spring Boot?

@RestController in Spring Boot is used to create RESTful web services. It simplifies the development process by eliminating the need for @ResponseBody on each method, automatically converting responses to JSON or XML, and providing easy integration with front-end applications.

Conclusion 

From the above discussion, we observe that @RestController is a convenient annotation that combines @Controller and @ResponseBody. Both annotations' primary purposes are to enable REST API processing of HTTP queries and to identify a class as a Spring-managed component. In this article, we discuss what @Controller and @RestController are. We also have discussed the difference between @Controller and @RestController.

Recommended Reading: AMD vs Intel

To learn more about this topic, check out the link- Implementing Validation for RESTful Services.

You can find more informative articles or blogs on our platform. You can also practice more coding problems and prepare for interview questions from well-known companies on your platform, Code360.

Live masterclass