ResponseEntity<T> is Spring's class for representing a complete HTTP response: the status code, the headers and the body. Return it from a controller method when you need to control any of those three parts at runtime, for example to answer 201 Created with a Location header, 404 Not Found with no body, or 304 Not Modified for caching.
A plain return value such as Product or @ResponseBody only lets you set the body, and the status is fixed at 200 OK. ResponseEntity removes that limit without any extra configuration in Spring MVC or Spring WebFlux.
ResponseEntity extends HttpEntity, which holds headers and a body, and adds an HTTP status. When a handler method returns it, Spring writes the status line, copies the headers, and serialises the body with the matching HttpMessageConverter (JSON through Jackson in most applications). Content negotiation still applies, so the Accept header of the request decides the format.
| Return type | Status | Headers | Body |
|---|---|---|---|
Product with @ResponseBody or @RestController | Always 200 (unless @ResponseStatus) | Defaults only | Yes |
ResponseEntity<Product> | Any, chosen per request | Any | Optional |
ResponseEntity<Void> | Any | Any | None |
The static factory methods and builders are the idiomatic way to create a response. They keep the code short and make the intent obvious.
ResponseEntity.ok(), created(URI), accepted(), noContent(), badRequest() and notFound() return builders. Call .body(...) to finish with a body or .build() to finish without one. For any other status, use ResponseEntity.status(HttpStatus.CONFLICT) or ResponseEntity.status(409).
Use the builder for headers instead of mutating a HttpServletResponse. Typical cases are Location after creation, ETag and Cache-Control for caching, and Content-Disposition for downloads.
For conditional requests, ResponseEntity.ok().eTag("\"v7\"") sets the ETag. Spring's ShallowEtagHeaderFilter can generate ETags automatically if you prefer.
@ResponseStatus fixes one status for a method or exception class at compile time. ResponseEntity decides at runtime, so it fits methods where the outcome varies. For failures, throwing an exception and handling it centrally is cleaner than returning error entities from every method.
ProblemDetail implements the RFC 9457 problem details format (which replaced RFC 7807) and is supported from Spring Framework 6. Handler methods in @RestControllerAdvice may also return ProblemDetail directly.
ResponseEntity<Object> everywhere. Use a specific type so the contract is visible and tests are simpler.200 OK with an error message in the body. Use a proper 4xx or 5xx status.null for the body of a ResponseEntity<Void>. Call build() instead.Location URI with string concatenation. Use ServletUriComponentsBuilder or UriComponentsBuilder so context paths and proxies are respected.@ResponseStatus on the method with a ResponseEntity return. The entity's status wins, so the annotation only confuses readers.No. For simple endpoints that always succeed, returning the object from a @RestController is enough. Use ResponseEntity when status or headers vary.
HttpEntity holds headers and a body. ResponseEntity extends it with an HTTP status code, and RequestEntity extends it with a method and URL for client calls.
Yes. Return Mono<ResponseEntity<T>> or ResponseEntity<Flux<T>>, and the same builders apply.
Use ResponseEntity.status(HttpStatus.ACCEPTED).build() or one of the shortcuts such as noContent().
ResponseEntity gives a controller full control over the HTTP status, headers and body of a response. Use the fluent builders for 201 Created with a Location, 404 without a body, downloads and cache headers. Keep simple success paths as plain return values, and handle errors with @RestControllerAdvice and ProblemDetail.