0
0
SpringbootComparisonBeginner · 3 min read

@RestController vs @Controller: Key Differences and Usage

@RestController is a specialized version of @Controller that automatically adds @ResponseBody to all methods, returning data directly as JSON or XML. @Controller is used for web pages and requires explicit @ResponseBody for REST responses.
⚖️

Quick Comparison

This table summarizes the main differences between @RestController and @Controller in Spring.

Aspect@RestController@Controller
PurposeBuild RESTful APIs returning data (JSON/XML)Build web applications returning views (HTML)
Response HandlingAutomatically serializes return values to response bodyReturns view names by default; needs @ResponseBody for data
Annotation CompositionCombines @Controller + @ResponseBodyOnly @Controller
Use CaseAPIs for frontend apps or servicesWeb pages with templates like Thymeleaf
Method Return TypeReturns data objects serialized as JSON/XMLReturns view names or data if annotated with @ResponseBody
⚖️

Key Differences

@RestController is a convenience annotation introduced in Spring 4. It combines @Controller and @ResponseBody, so every method returns data directly in the HTTP response body, typically as JSON or XML. This makes it ideal for building REST APIs where clients expect raw data.

In contrast, @Controller is designed for traditional web applications that return views like HTML pages. By default, methods return a view name that Spring resolves to a template. To return raw data from a @Controller, you must annotate the method with @ResponseBody explicitly.

Thus, the main difference lies in response handling: @RestController simplifies REST API development by removing the need to add @ResponseBody on each method, while @Controller offers more flexibility for mixed web applications that serve both views and data.

⚖️

Code Comparison

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

@RestController
public class ApiController {

    @GetMapping("/greeting")
    public String greeting() {
        return "Hello from RestController!";
    }
}
Output
Hello from RestController!
↔️

@Controller Equivalent

java
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ApiController {

    @GetMapping("/greeting")
    @ResponseBody
    public String greeting() {
        return "Hello from Controller!";
    }
}
Output
Hello from Controller!
🎯

When to Use Which

Choose @RestController when you are building RESTful APIs that return data like JSON or XML to clients such as web frontends or mobile apps. It simplifies code by automatically serializing responses.

Choose @Controller when you are developing traditional web applications that return HTML views or templates. Use @ResponseBody on specific methods if you need to return raw data alongside views.

Key Takeaways

@RestController is for REST APIs and returns data directly as JSON/XML.
@Controller is for web pages and returns view names by default.
@RestController combines @Controller and @ResponseBody automatically.
Use @ResponseBody on @Controller methods to return raw data.
Pick @RestController for APIs, @Controller for web UI with templates.