What is the difference between `@RestController` and `@Controller`?
The difference between @RestController
and @Controller
lies in how they handle HTTP responses:
@RestController
:- A convenience annotation that combines
@Controller
and@ResponseBody
. - It automatically serializes return values from handler methods into HTTP responses (usually JSON or XML).
- Used for building RESTful web services where responses are directly returned as data (not views).
- A convenience annotation that combines
@Controller
:- Marks a class as a Spring MVC controller that can handle web requests and return views (e.g., HTML pages).
- It does not automatically serialize return values. Typically, it returns view names (which are resolved by a view resolver) to render HTML.
In summary, use @RestController
for REST APIs that return data directly and @Controller
for traditional web applications that render views.