Blog Spring Boot

What is the difference between @RestController and @Controller in Spring Boot?

Here’s a breakdown of the key differences between @RestController and @Controller in Spring Boot, along with explanations and when to use each:

Key Differences

  • Purpose:
    • @Controller: Designed for traditional web applications that render views (e.g., HTML, JSP). You typically use it with view resolvers to map controller responses to specific view templates.
    • @RestController: Designed specifically for creating RESTful web services that return data (usually JSON or XML) directly to the client.
  • Implicit Annotations:
    • @Controller: Does not include any implicit annotations.
    • @RestController: A combination of @Controller and @ResponseBody. This means all methods in a @RestController class will automatically have the @ResponseBody annotation applied, indicating the return value should be serialized directly into the HTTP response body.
  • Response Handling:
    • @Controller: Relies on a view resolver to map the returned value to a view template to produce a rendered HTML response. You need to explicitly use @ResponseBody on methods if you want to return data directly.
    • @RestController: Methods automatically serialize returned objects into JSON or XML and write them directly to the HTTP response body. You don’t need to use @ResponseBody.

When to Use Which

  • @Controller: Choose this when:
    • You’re building a traditional web application with server-side rendered views (HTML, JSP, Thymeleaf, etc.).
    • You need flexibility in returning different types of views.
  • @RestController: Choose this when:
    • You’re building RESTful APIs that will be consumed by JavaScript frontends, mobile apps, or other API clients.
    • You primarily want to return data as JSON or XML.

Example

Java

// Traditional controller with view rendering
@Controller
public class BookController {
    @GetMapping("/books/{id}") 
    public String getBook(@PathVariable int id, Model model) { 
        Book book = findBookById(id);
        model.addAttribute("book", book);
        return "book-details"; // Returns 'book-details' view
    } 
}

// REST controller
@RestController
@RequestMapping("/api/books")
public class BookRestController {
    @GetMapping("/{id}")
    public Book getBook(@PathVariable int id) {
        return findBookById(id);  // Directly returns JSON representation 
    }
}

Summary Table

Feature@Controller@RestController
PurposeTraditional views, flexibilityRESTful web services
ImplicitNone@Controller and @ResponseBody
Response TypeView name (resolved by view resolver)Data (JSON, XML, etc.)
@ResponseBodyRequired on methods returning dataNot required

Let me know if you would like a more elaborate explanation, a specific use-case scenario, or have other related questions!

Avatar

Neelabh

About Author

As Neelabh Singh, I am a Senior Software Engineer with 6.6 years of experience, specializing in Java technologies, Microservices, AWS, Algorithms, and Data Structures. I am also a technology blogger and an active participant in several online coding communities.

You may also like

Blog Design Pattern

Understanding the Builder Design Pattern in Java | Creational Design Patterns | CodeTechSummit

Overview The Builder design pattern is a creational pattern used to construct a complex object step by step. It separates
Blog Tech Toolkit

Base64 Decode

Base64 encoding is a technique used to encode binary data into ASCII characters, making it easier to transmit data over