@Controller vs @RestController in Spring Boot – What You Need to Know

When building web applications with Spring Boot, choosing between @Controller and @RestController is more than just a matter of syntax—it shapes how your application handles HTTP responses. I’ve navigated this decision in multiple projects, and understanding the distinction helps avoid bugs and improves clarity.


🔹 What Is @Controller?

Use @Controller when your application needs to return views (HTML). Annotating a class with @Controller means:

  • Return values from handler methods are treated as view names.
  • A ViewResolver (e.g., Thymeleaf, JSP) processes that name, merges it with a model, and generates HTML.
  • No automatic JSON/XML conversion occurs—you manage data-to-view mapping yourself.
✅ When to Use It:
  • Traditional MVC apps.
  • Admin dashboards, server-rendered UIs.
  • Scenarios where you return views and UI templates.

🔹 What Is @RestController?

@RestController simplifies API development. It’s effectively the combination of @Controller + @ResponseBody, meaning:

  • Return values from methods are serialized directly to the HTTP response body (typically JSON or XML).
  • No view resolution—it’s solely for building RESTful APIs.
✅ When to Use It:
  • APIs returning JSON/XML.
  • Microservices, AJAX endpoints.
  • Any scenario where you’re building a RESTful interface.

🔁 Comparison Summary
Feature@Controller@RestController
Response TypeView name (HTML via ViewResolver)Serialized object (JSON/XML in body)
View Resolver✔ Yes✖ No
Need @ResponseBody?✔ For REST-like methods✖ Not needed (built-in)
Typical Use CasesMVC applications, template-driven UIREST API, data-only endpoints

💡 My Experience in Real Projects

In an e-commerce platform I worked on:

  • I used @Controller for admin-facing pages, rendering templates with Thymeleaf.
  • I switched to @RestController for AJAX-based endpoints—like loading order histories or updating statuses—returning JSON directly.
  • Mixing them without clear separation caused confusion: AJAX calls returned HTML pages unexpectedly. After refactoring, using distinct controllers for API and view logic brought clarity and reduced bugs.

✅ Best Practices
  1. Use the right tool for the job
    Pick @Controller for view-rendering, @RestController for APIs.
  2. Avoid mixing responsibilities
    Don’t return HTML in a RestController or vice versa—segregate UI and API layers.
  3. Use global error handling
    Combine @RestController with @ControllerAdvice for consistent, JSON-based error responses.
  4. Leverage content negotiation
    For hybrid controllers (rare), use produces/consumes to differentiate between JSON and HTML endpoints.

🎯 Conclusion

Although @Controller and @RestController might look similar, they fundamentally change how Spring interprets your handler methods:

  • @Controller → MVC, template-rendered responses.
  • @RestController → REST APIs, serialized JSON/XML.

Choosing the right one helps structure cleaner, safer, and more maintainable code. If you’re building both UI and API layers, separating them in different controllers is key to clarity and reliability.

Kuni
Kuni

Hi, I’m a developer based in South Korea. With years of experience in the tech industry, I am passionate about creating meaningful solutions and continually learning in this ever-evolving field.

I believe in the importance of leading a healthy and balanced economic life, and I aim to share insights, ideas, and practical tips to help others achieve the same. Through this blog, I hope to connect with like-minded individuals, exchange valuable knowledge, and grow together.

Let’s explore, learn, and build a thriving life together!

Let me know if you'd like further adjustments! 😊