The @ResponseBody annotation tells Spring MVC to write a controller method's return value straight into the HTTP response body, instead of treating it as the name of a view. Spring picks an HttpMessageConverter for the return type and the client's Accept header, so an object becomes JSON (or XML, or plain text) without any template.
It is the mechanism behind REST endpoints in Spring. In practice you rarely type it, because @RestController already applies it to every method.
In a classic @Controller, a returned String such as "home" is resolved to a view like home.html. With @ResponseBody, the same string is sent to the client as the body. For objects, Spring serialises them with a converter, usually Jackson for JSON.
GET /ping returns the text pong with Content-Type: text/plain.
Spring Boot auto-configures Jackson when it is on the classpath (the spring-boot-starter-web starter includes it), so no extra setup is needed. Records work with Jackson 2.12 and later.
Spring performs content negotiation: it compares the Accept header and the method's produces attribute with the converters registered in the application, then picks the first that can write the return type.
| Return type | Typical converter | Content type |
|---|---|---|
String | StringHttpMessageConverter | text/plain |
Object, record, List, Map | MappingJackson2HttpMessageConverter | application/json |
| Object with XML support on the classpath | Jackson XML converter | application/xml |
byte[], Resource | Byte array or resource converter | application/octet-stream or detected |
If no converter matches the requested type, the client gets 406 Not Acceptable.
@RestController is @Controller plus @ResponseBody. Put it on the class and you can drop the annotation on each method:
Use ResponseEntity<T> when you need to control the status code or headers per request. A method returning ResponseEntity already writes the body, so @ResponseBody is not needed on it even in a plain @Controller.
They are mirror images. @RequestBody reads the request body into an object with a converter, and @ResponseBody writes the return value out with a converter. Both rely on the same set of converters.
Exceptions thrown from these methods are not rendered as views. Handle them in a @RestControllerAdvice (which is @ControllerAdvice plus @ResponseBody):
Spring 6 also supports RFC 9457 ProblemDetail responses, which gives clients a standard error format. Return a ProblemDetail from the handler method, or enable spring.mvc.problemdetails.enabled=true in Boot.
String from a @Controller method without @ResponseBody makes Spring look for a view with that name, which causes a template error or a 404.void methods with @ResponseBody send an empty body with status 200.@JsonIgnore or dedicated response classes.No. @RestController already includes it, so every handler method writes to the response body.
@Controller returns view names by default, and @RestController returns data written to the body. The latter is the usual choice for JSON APIs.
Add an XML converter such as jackson-dataformat-xml to the classpath and request application/xml, or set produces on the mapping. Spring then selects the XML converter.
Yes, in the annotated-controller model. There handlers can also return Mono<T> or Flux<T>, and WebFlux writes them reactively.
@ResponseBody makes a Spring MVC method write its return value to the response body through a message converter instead of resolving a view. @RestController applies it for you, ResponseEntity adds status and header control, and @RestControllerAdvice keeps error responses consistent. Watch content negotiation, return DTOs, and avoid sending internal entities directly.