@Controller vs. @RestController Annotation in Spring Last Updated : 03 Sep, 2025 Comments Improve Suggest changes 25 Likes Like Report Spring Framework provides two commonly used annotations for handling web requests: @Controller and @RestController. They may look similar, but they are designed for different purposes:@Controller is typically used for MVC-based web applications (returning HTML views).@RestController is used for RESTful APIs (returning JSON/XML responses)@Controller Annotation@Controller is a Spring annotation used to define a web controller in an MVC (Model-View-Controller) application.It usually works with view resolvers (like Thymeleaf, JSP or FreeMarker) to return HTML pages.If you want to return JSON/XML data, you must use @ResponseBody with each method.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: 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. The RestController allows to handle all REST APIs such as GET, POST, Delete and PUT requests. It is equivalent to-> @Controller + @ResponseBodyExample: Using @RestController in a REST API Java @RestController public class ProductController { @GetMapping("/product") public Product getProduct() { return new Product(1, "Laptop", 80000); } } Explanation:The @RestController annotation combines @Controller and @ResponseBody.The getProduct() method returns a Product object that is automatically converted into JSON.@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.When to Use WhichUse @Controller when:You are building a web application that returns HTML pages.You want to integrate with frontend template engines like Thymeleaf or JSP.Use @RestController when:You are building REST APIs for mobile apps, SPAs or microservices.You need to exchange data in JSON/XML format. Create Quiz Comment A AmiyaRanjanRout Follow 25 Improve A AmiyaRanjanRout Follow 25 Improve Article Tags : Springboot Java-Spring Explore Spring Boot Basics and PrerequisitesIntroduction to Spring Boot4 min readDifference between Spring and Spring Boot4 min readSpring - Understanding Inversion of Control with Example6 min readSpring - IoC Container2 min readBeanFactory vs ApplicationContext in Spring6 min readSpring Boot CoreSpring Boot - Architecture2 min readSpring Boot - Annotations5 min readSpring Boot Actuator5 min readHow to create a basic application in Java Spring Boot3 min readSpring Boot - Code Structure3 min readSpring Boot - Scheduling4 min readSpring Boot - Logging8 min readException Handling in Spring Boot8 min readSpring Boot with REST APISpring Boot - Introduction to RESTful Web Services3 min readSpring Boot - REST Example4 min readHow to Create a REST API using Java Spring Boot?4 min readHow to Make a Simple RestController in Spring Boot?2 min readJSON using Jackson in REST API Implementation with Spring Boot3 min readSpring Boot with Database and Data JPA Spring Boot with H2 Database6 min readSpring Boot - JDBC8 min readAdvantages of Spring Boot JDBC3 min readSpring Boot - CRUD Operations7 min readSpring Boot - MongoRepository with Example5 min readSpring Boot JpaRepository with Example5 min readSpring Boot - CrudRepository with Example5 min readSpring Boot with KafkaSpring Boot Kafka Producer Example3 min readSpring Boot Kafka Consumer Example3 min readSpring Boot | How to consume JSON messages using Apache Kafka3 min readSpring Boot | How to consume string messages using Apache Kafka3 min readSpring Boot | How to publish String messages on Apache Kafka2 min readSpring Boot | How to publish JSON messages on Apache Kafka4 min readSpring Boot with AOPSpring Boot - AOP(Aspect Oriented Programming)4 min readHow to Implement AOP in Spring Boot Application4 min readSpring Boot - Difference Between AOP and OOP3 min readSpring Boot - Difference Between AOP and AspectJ3 min readSpring Boot - Cache Provider6 min read Like