Difference Between @Controller and @RestController Annotation in Spring Last Updated : 11 Mar, 2025 Comments Improve Suggest changes Like Article Like Report 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 directly affect the operation of the code they annotate. It does not change the action of the compiled program. Understanding the difference between @Controller and @RestController annotations in Spring is essential for developing web applications and RESTful APIs. Both annotations serve the purpose of handling web requests but differ in how they process and return responses.The main difference is:@Controller is used in Spring MVC applications to handle web requests and return views.@RestController is a specialized version of @Controller that combines @ResponseBody, making it ideal for RESTful APIs.@Controller vs @RestControllerThe table below demonstrates the differences between @Controller and @RestController annotations in Spring.Feature@Controller@RestControllerDefinitionThis annotation is used to mark classes as Spring MVC controllers.This is a specialized controller used for RESTful web services.InheritanceExtends @Component annotation.Extends @Controller annotation.View SupportReturns a view in Spring Web MVC.Cannot return a view.Response HandlingRequires @ResponseBody for sending responses as JSON or XML.Assumes @ResponseBody by default, eliminating the need for explicit annotations.Request HandlingUsed for traditional MVC applications where views (JSP, Thymeleaf) are returned.Used for REST APIs where JSON or XML responses are sent directly to the client.Spring VersionIntroduced in Spring 2.5.Introduced in Spring 4.0.@Controller AnnotationSpring @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.Example: Using @Controller in a Spring MVC Application Java @Controller public class HomeController { @GetMapping("/home") public String homePage(Model model) { model.addAttribute("message", "Welcome to Spring MVC!"); // Returns a view (home.html or home.jsp) return "home"; } } Explanation: In the above example,The @Controller annotation marks the class as a Spring MVC controller.The homePage() method maps to /home and returns a view named home.@RestController AnnotationThe @RestController annotation is used for making restful web services. 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. Example: Using @RestController in a REST API Java @RestController public class ProductController { @GetMapping("/product") public Product getProduct() { return new Product(1, "Laptop", 80000); } } Explanation: In the above example,The @RestController annotation combines @Controller and @ResponseBody.The getProduct() method returns a Product object that is automatically converted into JSON. Comment More infoAdvertise with us Next Article Difference Between @Controller and @RestController Annotation in Spring A AmiyaRanjanRout Follow Improve Article Tags : Java Difference Between Advance Java Java-Spring Practice Tags : Java Similar Reads Spring MVC CRUD with Example In this article, we will explore how to build a Spring MVC CRUD application from scratch. CRUD stands for Create, Read/Retrieve, Update, and Delete. These are the four basic operations to create any type of project. Spring MVC is a popular framework for building web applications. Spring MVC follows 7 min read Create and Run Your First Spring MVC Controller in Eclipse/Spring Tool Suite Spring MVC framework enables the separation of modules, namely Model, View, and Controller, and seamlessly handles the application integration. This enables the developer to create complex applications also using plain Java classes. The model object can be passed between the view and the controller 5 min read What is Dispatcher Servlet in Spring? Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made 6 min read Spring - Shortcut to Create Dispatcher Servlet in Eclipse/Spring Tool Suite Eclipse is an Integrated Development Environment (IDE) used in computer programming. It includes a base workspace and an extensible plug-in system for customizing the environment. It is the second-most-popular IDE for Java development. Eclipse is written mostly in Java and its primary use is for dev 4 min read WebApplicationContext in Spring MVC Spring MVC framework enables separation of modules namely Model, View, and Controller, and seamlessly handles the application integration. This enables the developer to create complex applications also using plain java classes. The model object can be passed between view and controller using maps. T 7 min read Spring - Multi Action Controller with Example Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made 4 min read Spring MVC - Exception Handling Prerequisites: Spring MVC When something goes wrong with your application, the server displays an exception page defining the type of exception, the server-generated exception page is not user-friendly. Spring MVC provides exception handling for your web application to make sure you are sending your 6 min read Spring - How to Load Literal Values From Properties File Literals in Java are a synthetic representation of boolean, numeric, character, or string data. It is a medium of expressing particular values in the program, such as an integer variable named ââ/count is assigned an integer value in the following statement. int x = 100; // Here 100 is a constant/li 4 min read Spring MVC - Multiple View Page A view page is redirected to another view page in this example. Let's look at a simple Spring Web MVC framework sample. The procedure is as follows: In the case of Maven, load the spring jar files or add dependencies.Make your controller class.Provide a controller entry in the web.xml file.In a sepa 3 min read Spring MVC - Custom Validation Validating user input is essential for any web application to ensure the processing of valid data. The Spring MVC framework supports the use of validation API. The validation API puts constraints on the user input using annotations and can validate both client-side and server-side. It provides stand 8 min read Like