DEV Community

Bishal Shrestha
Bishal Shrestha

Posted on

@Controller vs. @RestController

Controller vs. RestController Annotation Cover Image
In the realm of Spring framework development, distinguishing between traditional Spring MVC controllers and RESTful web service controllers is pivotal, particularly in their approach to handling HTTP response bodies.

Traditional Spring MVC Controller:

In the conventional Spring MVC paradigm, controllers rely on view technology, necessitating a distinct approach to managing HTTP response bodies compared to the more modern RESTful web service controllers.

RESTful Web Service Controller:

Conversely, RESTful web service controllers are designed to return objects directly, with their data serialized into the HTTP response, typically formatted as JSON or XML. The introduction of the @RestController annotation in Spring 4.0 streamlined the creation of RESTful web services by combining the functionalities of @Controller and @ResponseBody annotations.

Understanding Annotations:

@ResponseBody

To grasp the distinction between @Controller and @RestController, it's crucial to comprehend the role of the @ResponseBody annotation. This annotation signifies that the return value of a method should be bound to the web response body, facilitating automatic serialization of the returned object into the HTTP response.

@Controller

The @Controller annotation, a specialization of the @Component annotation, enables automatic detection of implementations through classpath scanning. When employing @Controller, it's imperative to include @ResponseBody in each @RequestMapping method to serialize the return value properly.

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/v1/api")
public class GreetingController {

    @GetMapping("/greet")
    @ResponseBody
    public String greet() {
        return "Hello, World!";
    }
}
Enter fullscreen mode Exit fullscreen mode

@RestController

Introduced to simplify the development of RESTful web services, the @RestController annotation serves as a specialized form of @Controller. It eliminates the need for manually adding @ResponseBody to each @RequestMapping method, as it inherently combines the functionalities of both @Controller and @ResponseBody.

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/v1/api")
public class GreetingRestController {

    @GetMapping("/greet")
    public String greet() {
        return "Hello, World!";
    }
}
Enter fullscreen mode Exit fullscreen mode

Key Takeaways:

While both @Controller and @RestController can be used together, they cater to different needs – @Controller for rendering views and @RestController for returning domain objects as JSON or XML.

Original post appeared here.

Top comments (0)