@Controller vs @RestController in Spring Boot

1. Introduction

In Spring Framework, @Controller and @RestController are annotations used to create web controllers. @Controller is used to mark a class as a web request handler, a part of Spring MVC that allows you to create views. @RestController combines @Controller and @ResponseBody, indicating that the return value of the methods should be bound to the web response body and is typically used for APIs.

2. Key Points

1. @Controller is a stereotype annotation indicating the class is a web controller.

2. @RestController is a specialized version of the controller that is a convenience annotation for creating RESTful controllers.

3. @Controller is typically used in combination with @ResponseBody or a view resolver for rendering views.

4. @RestController assumes every method returns a domain object instead of a view, which is directly written to the HTTP response as JSON or XML.

3. Differences

@Controller @RestController
A specialization of @Component. Marks a class as a web controller, primarily used for handling HTTP requests and returning views. A convenience annotation that acts as a shorthand for @Controller and @ResponseBody. It indicates that the class is a controller where every method returns a domain object instead of a view.
Typically used to return a view (like JSP or Thymeleaf). It’s ideal for web applications that need to render HTML pages. Simplifies the creation of RESTful web services. Every method returns the object and object data is directly written into HTTP response as JSON or XML.
Methods can return ModelAndView, String, or any other data type. When returning objects as responses for API calls, methods should be annotated with @ResponseBody. Methods assume @ResponseBody semantics by default, meaning they don’t return views but rather write directly to the body of the response.
Used when the application needs to directly manipulate the model and view technologies like JSP, Thymeleaf, etc. Used for API layers and microservices, where the response is data (JSON/XML) rather than a rendered view.

4. Example

// Example using @Controller
@Controller
public class MyViewController {

    @RequestMapping("/greeting")
    public String getGreeting(Model model) {
        model.addAttribute("message", "Hello World");
        return "greeting"; // Name of the view
    }
}

// Example using @RestController
@RestController
public class MyRestController {

    @RequestMapping("/data")
    public MyData getData() {
        return new MyData("Hello World"); // Data is directly written as JSON
    }
}

public class MyData {
    private String message;
    // Constructor and getter
}

Output:

// Output from @Controller would be the rendered view greeting.
// Output from @RestController would be the JSON string: {"message":"Hello World"}

Explanation:

1. MyViewController with @Controller is a traditional Spring MVC controller that returns a view name to be resolved and rendered.

2. MyRestController with @RestController directly returns a MyData object that will be automatically converted to JSON format and sent in the response body.

5. When to use?

- Use @Controller when building a web application that renders HTML views.

- Use @RestController when developing a RESTful web service that returns data in a format such as JSON or XML for consumption by various clients.

Comments