ResponseEntity is a Spring class that represents a complete HTTP response: the status code, the headers and the body. You return it from a controller method when you need control over more than the body, for example to send 201 Created with a Location header or 404 Not Found with no body. It extends HttpEntity and is generic in the body type, as in ResponseEntity<User>.
If every response from a method is a plain 200 OK with a body, you do not need it: a @RestController method that returns the object directly is enough. Reach for ResponseEntity when status or headers depend on what happens at runtime.
A ResponseEntity wraps three things:
| Part | Type | Purpose |
|---|---|---|
| Status | HttpStatusCode | The numeric status such as 200, 201 or 404 |
| Headers | HttpHeaders | Metadata such as Location, ETag, Cache-Control |
| Body | T | The object that an HttpMessageConverter writes (JSON by default with Jackson) |
Since Spring Framework 6, getStatusCode() returns HttpStatusCode, which can hold any status number, not only the values of the HttpStatus enum. The old getStatusCodeValue() is gone, so call getStatusCode().value() if you need the integer.
The constructors still work, but the static builders are shorter and harder to get wrong. Prefer them in new code.
The getUser method can be shortened further with ResponseEntity.of(Optional<T>), which returns 200 with the value or 404 when the optional is empty.
The common builders are ok(), created(URI), accepted(), noContent(), badRequest(), notFound(), unprocessableEntity() and status(HttpStatusCode) for anything else.
Use the builder's header(...) and headers(...) methods, or the dedicated helpers for common headers such as eTag, lastModified, cacheControl and contentType.
This is also the standard way to return files and streams: set Content-Type, Content-Disposition and return a Resource or byte[] as the body. For long downloads, ResponseEntity<StreamingResponseBody> avoids loading the whole file into memory.
| Approach | Status decided | Headers | Best for |
|---|---|---|---|
| Return the object | Always 200 | None | Simple reads that cannot fail in interesting ways |
@ResponseStatus on the method | At compile time | None | A fixed status such as 201 or 204 |
ResponseEntity | At runtime | Full control | Conditional statuses, Location, caching, downloads |
@ResponseStatus is declarative and fixed, while ResponseEntity is programmatic. You can mix them in one application: use the annotation where the status never changes and ResponseEntity where it does.
You can return error bodies directly from a handler, but centralising them is cleaner. A @RestControllerAdvice with @ExceptionHandler methods can return a ResponseEntity per exception type.
ProblemDetail (Spring 6 and later) models the standard problem-details format for HTTP APIs defined in RFC 9457, which replaces RFC 7807. It is serialised with the media type application/problem+json. Setting spring.mvc.problemdetails.enabled=true in Spring Boot makes the built-in MVC exceptions use it too.
ResponseEntity<String> when you meant JSON. A String body is written as plain text, so return an object or a record.new ResponseEntity<>(HttpStatus.OK) with a generic mismatch. The builders infer the type for you.ResponseEntity.ok(...). It adds noise without changing behaviour; return the object directly.@RestControllerAdvice.ResponseEntity<Void> still needs .build() rather than .body(null) for an empty body.No. Returning a plain object produces 200 OK with the object serialised in the body. Use ResponseEntity only when the status or headers vary.
HttpEntity holds headers and a body and is also used for requests. ResponseEntity extends it and adds a status code, so it can describe a full response.
Yes. Return Mono<ResponseEntity<T>> in annotated WebFlux controllers to set the status and headers while the body is produced reactively.
Return ResponseEntity.notFound().build(), use ResponseEntity.of(optional), or throw an exception that a @RestControllerAdvice maps to 404.
ResponseEntity gives you the whole HTTP response in one object: status, headers and body. Use the builder methods such as ok(), created() and noContent(), set headers through the fluent API, and keep error mapping in a @RestControllerAdvice that returns ProblemDetail. When the response is always a plain 200, return the object directly.