'Coding has over 700 languages', '67% of programming jobs aren’t in the technology industry', 'Coding is behind almost everything that is powered by electricity'
Last Updated: Jan 1, 2024
Easy

Difference between @Controller and @RestController

Author Vidhi Sareen
0 upvote
gp-icon
Basics of javascript
Free guided path
10 chapters
68+ problems
gp-badge
Earn badges and level up

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 
}
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 
}

Comparison Table 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.

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, Coding Ninjas Studio.

Previous article
Implementing Exception Handling - 404 Resource Not Found
Next article
Implementing DELETE Method to Delete a User Resource
Guided path
Free
gridgp-icon
Basics of javascript
10 chapters
68+ Problems
gp-badge
Earn badges and level up
Live masterclass