@ResponseBody tells Spring MVC to write a controller method's return value directly into the HTTP response body instead of treating it as a view name. Spring picks an HttpMessageConverter that serialises the object, typically to JSON, according to the client's Accept header. In practice you rarely write it yourself, because @RestController is @Controller plus @ResponseBody on every method.
The examples use Spring Boot 3 and Java 17 or newer.
In a classic @Controller, a returned String is a view name that a ViewResolver turns into a template such as home.html. With @ResponseBody, the return value is the response content itself.
The first method renders a template; the second returns JSON. The annotation can sit on a method or on the whole class.
After the handler returns, RequestResponseBodyMethodProcessor negotiates the content type and chooses the first HttpMessageConverter that can write the value.
| Return type | Typical converter | Result |
|---|---|---|
String | StringHttpMessageConverter | Plain text |
byte[], Resource | Byte array and resource converters | Raw bytes, files |
Object, record, List, Map | Jackson JSON converter | JSON |
| Object with XML library on classpath | Jackson XML or JAXB converter | XML |
Spring Boot registers the JSON converter automatically when Jackson is on the classpath, which spring-boot-starter-web provides. You can restrict or force the type with produces.
If the client asks for a type the server cannot produce, Spring returns 406 Not Acceptable.
@RestController is a meta-annotation combining @Controller and @ResponseBody. Every method in the class then writes to the body, so you do not repeat the annotation.
Use @Controller with @ResponseBody on selected methods only when one class mixes HTML pages and JSON endpoints. For anything that is purely an API, @RestController is the standard choice.
@ResponseBody alone returns 200 OK with default headers. To set a status code, use @ResponseStatus for a fixed status or return a ResponseEntity for dynamic control.
ResponseEntity lets you choose status, headers and body at runtime and is the better tool when different branches return different codes. A void method with @ResponseBody produces an empty body, and returning null does the same.
HttpMessageNotWritableException or No converter found.String return value is written as text, not JSON. To send a JSON string literal, return an object or set produces and serialise yourself.@ResponseBody on a method that should render a page makes the browser display the view name as text.No. @RestController already applies it to all handler methods.
@ResponseBody writes the return value to the response, while @RequestBody reads the request body into a method parameter. Both use HttpMessageConverter.
Add @ResponseBody to the method (or switch the class to @RestController) and return an object. Jackson serialises it when it is on the classpath.
You are probably using @ResponseBody or @RestController on a method that should return a template. Use a plain @Controller for views.
@ResponseBody makes Spring write a handler's return value to the response body through an HttpMessageConverter, usually as JSON. @RestController applies it to a whole class. Use @ResponseStatus or ResponseEntity when you need a status other than 200, and return DTOs rather than persistence entities.