@ResponseBody tells Spring MVC to write a controller method's return value straight into the HTTP response body, instead of treating it as a view name. Spring picks an HttpMessageConverter that matches the return type and the client's Accept header, so a Java object usually becomes JSON.
You rarely write it on every method today, because @RestController already includes it. You still meet it in classic @Controller classes that mix HTML pages and JSON endpoints.
In a plain @Controller, a String return value is a logical view name that a ViewResolver turns into a template such as a Thymeleaf page. Adding @ResponseBody switches that behaviour off for the method. The value is handed to HttpMessageConverter implementations, which serialise it and set the Content-Type header.
A request to /ping returns the text pong with Content-Type: text/plain. A request to /home renders the home template.
When a method returns a non-string object, Spring Boot's auto-configured Jackson converter writes JSON. A Java record keeps the example short and needs no getters or setters.
Collections work the same way: a List<Product> becomes a JSON array, and a Map<String, Object> becomes a JSON object. Jackson 2 is the default in Spring Boot 3; check the release notes of your Spring Boot version if you upgrade to a newer major line, because the default JSON library can change between majors.
DispatcherServlet calls your handler method.RequestResponseBodyMethodProcessor sees @ResponseBody and takes over from view resolution.Accept header and the produces attribute of the mapping to choose a media type (content negotiation).HttpMessageConverter that can write the return type to that media type serialises the value.Content-Type.If no converter fits, Spring answers with 406 Not Acceptable (or 500 if the failure happens while writing). You can force a type with produces:
@RestController is a meta-annotation that combines @Controller and @ResponseBody. Every handler method in the class behaves as if it were annotated with @ResponseBody, so the annotation is redundant there.
| Situation | Use |
|---|---|
| A class that only serves JSON or other data | @RestController |
| A class that mostly renders views with a few data endpoints | @Controller plus @ResponseBody on those methods |
| A class where you need full control of status and headers | Return ResponseEntity<T> (it does not need @ResponseBody) |
@ResponseBody can also be placed on a class, and it works on @ControllerAdvice handlers, so an @ExceptionHandler can return an error object as JSON.
@ResponseBody always yields 200 unless you say otherwise. Use @ResponseStatus for a fixed status, or return ResponseEntity to decide per request.
Do not confuse @ResponseBody (outgoing data) with @RequestBody (incoming data). They are independent and use the same message converters in opposite directions.
No. @RestController already applies it to every method.
Yes, if an XML-capable converter is on the classpath, such as Jackson's XML module, and the client sends a matching Accept header.
For a @ResponseBody method that returns null or void, Spring sends an empty body with status 200 unless you set another status.
Annotated controllers in WebFlux support it, and values such as Mono<T> and Flux<T> are written through reactive encoders instead of servlet message converters.
@ResponseBody bypasses view resolution and serialises the return value with a message converter chosen by content negotiation. Prefer @RestController for API classes, return ResponseEntity when you need control over status and headers, and use produces to pin the media type.