@RestController marks a class as a Spring web controller whose methods write their return values directly to the HTTP response body. It is a shortcut for @Controller plus @ResponseBody, so you no longer annotate every method. Return values such as objects, records and lists are serialized to JSON by Jackson (or XML if configured) by default in Spring Boot.
It is the standard way to build REST APIs in Spring Boot, and it is available in both Spring MVC and Spring WebFlux.
The annotation is declared in the Spring source as a meta-annotation that combines two others:
Because @Controller is itself a @Component stereotype, the class is detected by component scanning and registered as a bean. @ResponseBody on the type applies to every handler method inside it.
| Aspect | @Controller | @RestController |
|---|---|---|
| Return value | View name by default | Written to the response body |
Needs @ResponseBody per method | Yes, for JSON | No |
| Typical use | Server-rendered pages (Thymeleaf) | JSON or XML APIs |
| Can mix both styles in one class | Yes | No, everything is a body |
If one class must render both HTML views and JSON, keep @Controller and add @ResponseBody only on the JSON methods, or split it into two classes.
This example uses a record as the response type, constructor injection, and the composed mapping annotations. It runs as-is in a Spring Boot 3.x or 4.x web application.
A request to GET /api/users/1 returns:
@RequestBody reads the JSON request into a CreateUser record, and the returned UserDto is serialized back to JSON without any view resolution.
Spring picks the response format from the Accept header and the converters on the classpath. Jackson handles JSON, and adding jackson-dataformat-xml enables XML. You can restrict a method with produces:
For errors, add one @RestControllerAdvice that maps exceptions to ProblemDetail responses so every endpoint fails in the same format. A @RestControllerAdvice is itself @ControllerAdvice plus @ResponseBody, mirroring how @RestController is built.
@Valid and Jakarta Bean Validation annotations.ResponseEntity only when status or headers vary; otherwise return the object.Yes. It is a composed annotation that applies both, so every method writes its return value to the response body.
Not in the usual way. A returned String is written as text rather than resolved as a view name. Use @Controller for views.
Yes. The same annotation works in reactive apps, where methods return Mono or Flux.
Configure Jackson through spring.jackson.* properties or by defining a Jackson2ObjectMapperBuilderCustomizer bean (or the equivalent customizer for the Jackson version your Boot release uses).
@RestController combines @Controller and @ResponseBody so handler methods return data instead of views. It removes repetitive annotations, enables automatic JSON serialization, and is the base of most Spring Boot REST APIs. Pair it with DTO records, validation and a central @RestControllerAdvice for clean, consistent endpoints.