@ResponseStatus sets the HTTP status code that Spring returns for a controller method or for an exception. On a handler method it replaces the default 200 OK, so a create endpoint can answer 201 Created and a delete endpoint 204 No Content with no extra code. On an exception class it maps the exception to a status when it is thrown.
This article covers the method-level use and the details that surprise developers, such as what happens with void methods and when the status is applied.
Without the annotation, a successful handler returns 200. Add the annotation to change that.
A client calling POST /api/orders receives:
The status is applied when the method returns normally. If the method throws, the annotation on the method is not used; the exception handling decides the status.
Since Spring 4.2 you can annotate a class to give all its methods a default status. A method-level annotation overrides it. This is rarely useful in REST controllers, because most handlers differ, so prefer method-level annotations.
You can combine @ExceptionHandler with @ResponseStatus to fix the status of the error response while the method builds the body:
Here the body is JSON written by the message converters and the status is 400. Note that setting reason in this position would route the response through the servlet sendError mechanism and you would lose your own body, so leave reason out when you return a body.
| Need | Use |
|---|---|
| Always the same non-200 status, such as 201 or 204 | @ResponseStatus |
Status depends on logic, or you need headers such as Location | ResponseEntity |
| Map an exception to a status | @ResponseStatus on the exception, ResponseStatusException or an advice |
If you set both, an explicit status in a ResponseEntity takes precedence over the annotation, so avoid mixing them in one method; it only confuses readers.
@ResponseStatus(NOT_FOUND) on a normal method. The status is applied on successful return, so the client gets a 404 even though the lookup worked. Use it for success codes, and throw for failures.204. A 204 response must not have a body, so use void.reason and then wondering why the body is Boot's default error JSON. reason triggers sendError.@RestController or @ResponseBody and expecting JSON. The annotation only sets the status; how the return value is written is decided by the controller type.HttpStatus.OK. It is the default, so the annotation adds nothing.@MockitoBean replaces @MockBean from Spring Framework 6.2 and Spring Boot 3.4; on older versions use @MockBean.
200 OK with an empty body by default. Add @ResponseStatus(HttpStatus.NO_CONTENT) to return 204.
Not with the annotation. Return a ResponseEntity or inject HttpServletResponse and call setStatus.
They are aliases for the same attribute. Write @ResponseStatus(HttpStatus.CREATED) or @ResponseStatus(code = HttpStatus.CREATED).
Yes, annotated WebFlux controllers support @ResponseStatus as well.
Use @ResponseStatus on handler methods for fixed success codes such as 201 and 204, and on exceptions for simple status mapping. Switch to ResponseEntity when the status varies, and avoid the reason attribute when you want your own response body.