ResponseEntity is the Spring class that models a whole HTTP response: the status code, the headers and the body. A controller method returns it when the response depends on runtime logic, such as 201 Created with a Location header after an insert, or 404 Not Found when a record is missing. For fixed responses, a plain return type is simpler.
This answer focuses on how ResponseEntity behaves across Spring MVC and how it compares with the alternatives, using Spring Boot 3.x and 4.x with Java 21 or later.
ResponseEntity<T> is a generic class in org.springframework.http. It extends HttpEntity<T> (headers plus body) and adds an HttpStatusCode. When a controller method returns one, Spring writes the status, copies the headers, and passes the body to an HttpMessageConverter, usually Jackson for JSON.
The same method with a plain User return type could only produce 200, or an exception for the missing case.
| Situation | Builder call | Status |
|---|---|---|
| Resource read | ResponseEntity.ok(body) | 200 |
| Resource created | ResponseEntity.created(uri).body(body) | 201 |
| Accepted for async work | ResponseEntity.accepted().body(body) | 202 |
| Success, nothing to return | ResponseEntity.noContent().build() | 204 |
| Not found | ResponseEntity.notFound().build() | 404 |
| Invalid input | ResponseEntity.badRequest().body(error) | 400 |
| Any other code | ResponseEntity.status(409).body(error) | any |
Headers are set on the builder before the body. You can use HttpHeaders for several at once.
The constructor form new ResponseEntity<>(body, headers, status) and the builder form are equivalent. The builder is easier to read and is the usual choice.
| Approach | Best for | Limitation |
|---|---|---|
| Plain return type | Always the same response | Cannot vary status or headers |
@ResponseStatus | A fixed non-200 status | Fixed per method |
ResponseStatusException | Throwing an error from deep code | Error path only |
@ExceptionHandler or @RestControllerAdvice | Uniform error format | Separate from the happy path |
ResponseEntity | Status or headers decided at runtime | More verbose |
A common pattern is to return ResponseEntity for success variations and let exceptions flow to a @RestControllerAdvice that returns ProblemDetail for errors.
MockMvc lets you verify status, headers and body without starting a server.
@MockitoBean replaces the older @MockBean, which is deprecated in recent Spring Boot versions.
Use it when the status code or headers change depending on the request or the data. If every call returns 200 with the same headers, return the object directly.
Yes. ResponseEntity.noContent().build() and ResponseEntity.notFound().build() send no body. Declare ResponseEntity<Void> when a body is never sent.
Instances are effectively immutable once built, since the builder produces a final object. You create a new one for each response.
HttpEntity holds headers and a body and is used for both requests and responses. ResponseEntity adds a status code and is meant only for responses.
ResponseEntity is the tool for building the full HTTP response in a Spring controller. Use its builders for correct status codes and headers, keep fixed responses simple with plain return types or @ResponseStatus, and centralize error responses in a @RestControllerAdvice.