ResponseEntity<T> represents the complete HTTP response in Spring: status code, headers and body. You return it from a controller method when a plain return value is not enough, for example to send 201 Created with a Location header, a 404 with no body, or custom caching headers. It lives in org.springframework.http and works in Spring MVC and, for servlet-based apps, in Spring Boot REST controllers.
Without ResponseEntity, a method that returns an object always produces 200 OK (or whatever @ResponseStatus declares), and headers are fixed. With it, every part of the response is decided per request.
ResponseEntity extends HttpEntity, which carries headers and a body, and adds the status code.
| Part | What you control | Example |
|---|---|---|
| Status | Any HttpStatusCode | 200, 201, 204, 404, 409 |
| Headers | Any response header | Location, ETag, Cache-Control, Content-Disposition |
| Body | Any object a message converter can write | A DTO, a list, a Resource, byte[] |
The body is converted by the same HttpMessageConverter infrastructure used for @ResponseBody, so JSON serialization with Jackson works as usual.
The static factory methods cover the common cases and keep the intent readable.
Here get returns 200 with a body or 404 with none. create returns 201 Created and a Location header pointing at the new resource, which is the REST convention. delete returns 204 No Content.
Use ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(id).toUri() instead of a hard-coded path when you want the URI built from the actual request.
The builder form lets you chain header settings before the body.
Spring can also answer conditional requests (If-None-Match) with 304 Not Modified when you set an ETag or last-modified value and use the request-checking support.
For one-off errors you can return ResponseEntity.status(...) with a body. For consistent error output across the whole API, prefer ProblemDetail (RFC 9457 format), which Spring supports directly.
Throwing ResponseStatusException is another light option for simple status codes. Reserve ResponseEntity returns for the happy-path variations.
If a method always returns the same status and no special headers, a plain return type is shorter.
A fixed status can be declared with @ResponseStatus. Use ResponseEntity when the status or headers depend on runtime logic.
@ResponseBody writes the return value as the body and nothing more. ResponseEntity wraps the body together with the status and headers, so you control the whole response.
In Spring WebFlux you return Mono<ResponseEntity<T>> or ResponseEntity<Flux<T>>. The same class is used, but the body may be a reactive type.
No. Use it when the status code or headers vary at runtime. Otherwise plain return types with @ResponseStatus are simpler.
Use ResponseEntity.noContent().build() for 204, or ResponseEntity.notFound().build() for 404. Declare the type as ResponseEntity<Void> when no body is ever sent.
ResponseEntity gives you full control over the HTTP status, headers and body of a response. Use its fluent builders for 201 Created with Location, 204 No Content, cache headers and conditional responses, use ProblemDetail for uniform error payloads, and fall back to plain return types when the response is always the same.