ResponseEntity is Spring's class for representing a complete HTTP response: the status code, the headers and the body. You return it from a controller method when a plain return value is not enough, for example when the status depends on the outcome, when you must set headers such as Location or ETag, or when the body can be empty. It is available in Spring MVC and Spring WebFlux and is the standard return type for REST endpoints that need precise control.
If a handler always answers 200 OK with a body, returning the object directly from a @RestController is simpler. Reach for ResponseEntity as soon as the response itself becomes part of your logic.
ResponseEntity<T> extends HttpEntity<T>, which holds headers and a body, and adds an HTTP status. Spring's HttpEntityMethodProcessor recognises it as a return value, writes the status and headers to the servlet response, then converts the body with the configured HttpMessageConverter (Jackson for JSON by default).
| Piece | How you set it | Example |
|---|---|---|
| Status | ResponseEntity.status(...) or a shortcut such as ok() | 201 Created |
| Headers | .header(...), .headers(...), .location(...), .eTag(...) | Location: /api/books/7 |
| Body | .body(...) | a record, list or String |
The static factory methods return a builder, so most responses are one expression. This controller shows the common cases.
The GET /api/books/{id} method returns 200 with the book or 404 with an empty body. POST answers 201 Created with a Location header pointing to the new resource, which is the REST convention. DELETE returns 204 No Content. When the value is an Optional, ResponseEntity.of(optional) does the same mapping in one call.
A successful response looks like this:
Use ResponseEntity.status(...) for any status code and add headers through the builder. Spring provides typed helpers for common ones.
Setting an ETag lets clients send If-None-Match, and Spring can answer 304 Not Modified when you use ServletWebRequest.checkNotModified(...) or the ShallowEtagHeaderFilter.
Returning ResponseEntity for errors works, but repeating badRequest().body(...) in every method scatters error handling. Prefer one @RestControllerAdvice that returns ResponseEntity built from ProblemDetail, the RFC 9457 error format supported since Spring Framework 6.
For simple cases you can also throw ResponseStatusException from the handler and let Spring produce the response. Setting spring.mvc.problemdetails.enabled=true in Spring Boot makes the built-in exceptions use the same format.
| Approach | Best for | Limitation |
|---|---|---|
Return the object from @RestController | Always 200 OK with a body | No control over status or headers |
@ResponseStatus(HttpStatus.CREATED) | A fixed status | Cannot vary at runtime or set headers |
ResponseEntity<T> | Dynamic status, headers, optional body | Slightly more code |
ResponseStatusException | Failing fast with an error status | Only for error paths |
StreamingResponseBody inside ResponseEntity | Large downloads written directly to the output stream | Needs care with timeouts |
ResponseEntity<String> with JSON text you built by hand. Return a typed object and let the message converter serialise it.ResponseEntity. Always give the type parameter so the intent and OpenAPI documentation stay clear.200 with an error message in the body. Use a real 4xx or 5xx status so clients and monitoring can react.ResponseEntity in the service layer. Keep it in the web layer and let services return domain objects or throw exceptions.Use it when the status code or headers depend on runtime conditions, or when the body can be absent. If every successful call is a plain 200 OK with a body, returning the object is enough.
Yes. Use ResponseEntity.noContent().build() for 204, ResponseEntity.notFound().build() for 404, or ResponseEntity<Void> as the return type. Calling .build() finishes a builder that has no body.
@ResponseBody (included in @RestController) only tells Spring to write the return value as the body. ResponseEntity also carries the status and headers, so it describes the whole response.
Yes. In Spring WebFlux you can return Mono<ResponseEntity<T>> or ResponseEntity<Flux<T>>, depending on whether the status is decided asynchronously or the body is a stream.
ResponseEntity gives a controller full control over status, headers and body. Use the fluent builders such as ok(), created(uri), noContent() and notFound() for clear REST semantics, keep error formatting in a @RestControllerAdvice with ProblemDetail, and return plain objects when no customisation is needed.