Difference Between @Controller And @RestController In Spring Boot

    Difference Between @Controller And @RestController In Spring Boot

    This article explains the differences between @Controller and @RestController in Spring Boot. It covers their use cases, response handling, and how to choose the right annotation for web applications and RESTful APIs.

    default profile

    Shreya Adak

    February 14, 2025

    2 min read

    Introduction:#

    In Spring Boot, @Controller and @RestController annotations are used in Spring MVC for handling HTTP requests.

    Annotations in Spring Framework and Java: Annotations provide a couple/bunch of codes that are already in-built. Annotations help configure beans, services, controllers, transactions, validations, and much more. Annotations are special metadata. It helps to reduce the code repetition.

    • Less XML configuration: The application becomes more intuitive and cleaner.
    • Automatic Configuration: Spring Boot automatically detects and configures beans with annotations.
    • Simplified Dependency Injection: Annotations like @Autowired eliminating manual wiring.

    @Controller:#

    In Spring MVC, @Controller is used to create web-based applications that return views (HTML, JSP, Thymeleaf, etc.). It is typically used for handling UI-based requests. Requires @ResponseBody if you want to return JSON or XML instead of a view. It is used when building traditional web applications with templates (Thymeleaf, JSP).

    @Controller public class WebController { @GetMapping("/home") public String homePage() { return "home"; // Returns "home.html" or "home.jsp" from templates } @GetMapping("/json") @ResponseBody // Needed to return JSON public Map<String, String> getJson() { return Map.of("message", "Hello from Controller"); } }

    @RestController:#

    In Spring MVC, it is a combination of @Controller and @ResponseBody, meaning every method automatically returns JSON or XML data. Used when building REST APIs instead of serving web pages. It is used when creating REST APIs that return JSON/XML.

    @RestController public class ApiController { @GetMapping("/api/data") public Map<String, String> getData() { return Map.of("message", "Hello from RestController"); } }

    Difference Between @Controller And @RestController In Spring Boot:#

    Feature@Controller@RestController
    PurposeHandles web page requestsHandles REST API requests
    ReturnsView (HTML, JSP, etc.)JSON/XML response
    @ResponseBody Needed?Yes (for JSON)No (applied automatically)
    Common Use CaseMVC-based web appsRESTful web services

    Conclusion:#

    We learned that the @Controller is used for handling web requests that return views in MVC applications, and Spring, @RestController is designed for building RESTful APIs that return data in formats like JSON or XML. Understanding these distinctions helps you choose the right annotation for your application needs.

    Spring
    Spring Boot
    Controller
    Annotations

    More articles