What is the difference between @Controller and @RestController?

Table of Contents

Introduction

In Spring Framework, @Controller and @RestController are both used to define controller classes that handle HTTP requests. However, there are key differences between these two annotations that affect how responses are returned and how the controller interacts with the client. In this guide, we’ll explore the distinctions between @Controller and @RestController, providing examples and explaining when to use each.


What is @Controller?

@Controller is a Spring annotation used to define a controller class in a Spring MVC application. It is a specialized version of the @Component annotation, meaning Spring will treat any class annotated with @Controller as a bean and manage it as part of the Spring context.

Key Features of @Controller:

  • View-based Response: By default, @Controller is used to handle HTTP requests that return views (such as JSP, Thymeleaf templates, or other UI elements).
  • Model-View-Controller (MVC): It is typically used in traditional web applications where a response is returned as a view that the browser can render.
  • Support for View Resolution: A method in a controller annotated with @Controller can return a String, which is interpreted as a logical view name. The view resolver will then map this to an actual view (e.g., a JSP or HTML page).

Example of @Controller

In the example above:

  • The @Controller annotation indicates that the class is a Spring MVC controller.
  • The greet() method handles HTTP GET requests to /greet and returns a view named "greet".

What is @RestController?

@RestController is a convenience annotation introduced in Spring 4. It is essentially a combination of @Controller and @ResponseBody. This means that:

  • **@RestController** automatically serializes the return value of the methods as JSON or XML, depending on the client's Accept header.
  • No View Resolution: Unlike @Controller, methods in a @RestController return data directly (usually in the form of JSON or XML) rather than a view name.
  • Ideal for REST APIs: It is commonly used in RESTful web services where responses are sent as JSON (or other data formats), rather than views.

Key Features of @RestController:

  • Automatic **@ResponseBody**: Every method in a @RestController is implicitly annotated with @ResponseBody, which tells Spring to write the return value directly to the HTTP response body.
  • Ideal for REST APIs: It simplifies the process of creating RESTful web services in Spring, where the server needs to return data (often in JSON format) rather than rendering views.

Example of @RestController

In the example above:

  • The @RestController annotation means that this class is handling REST API requests.
  • The greet() method returns a simple string, and Spring automatically converts it to JSON (or sends as plain text if JSON is not required).

Key Differences Between @Controller and @RestController

Feature@Controller@RestController
PurposeUsed for MVC applications with views (JSP, Thymeleaf, etc.).Used for REST APIs to return data (usually JSON or XML).
Response TypeTypically returns a view name, and the view resolver handles rendering the response.Automatically returns response data (JSON, XML) via @ResponseBody.
View ResolutionReturns a logical view name that is resolved by a view resolver.Does not involve view resolution; returns data directly.
**@ResponseBody**Must be explicitly used on methods if returning data directly.Automatically applies @ResponseBody to all methods.
Use CaseSuitable for web applications that need to render views.Suitable for creating RESTful web services that return data.

Practical Examples

Example 1: Using @Controller for Web Page Rendering

If you're building a traditional web application with Spring MVC, you’ll use @Controller to return views.

Example 2: Using @RestController for RESTful API Responses

For creating a REST API where you send data to clients (e.g., JSON), you’ll use @RestController.

In the second example, the response is automatically converted into JSON format, and no view is involved.

Conclusion

The primary difference between @Controller and @RestController lies in how they handle responses:

  • @Controller is used for rendering views in traditional web applications (with a view resolver), where the server responds with HTML, JSP, or Thymeleaf templates.
  • @RestController, on the other hand, is used for building RESTful web services where the server responds with data in formats like JSON or XML, typically for APIs.

Choosing between the two depends on whether you're building a web application that renders views or a RESTful API that sends data.

Similar Questions