@ResponseStatus is a Spring annotation that binds an HTTP status code to a controller method or to an exception class. When a method annotated with it returns normally, or when an annotated exception propagates out of a handler, Spring sends that status instead of the default. It is the simplest declarative way to turn exceptions into proper 4xx responses.
It is intentionally small: one status and an optional reason, decided at compile time. When you need a dynamic status or a structured error body, combine it with @ExceptionHandler or return a ResponseEntity.
The annotation lives in org.springframework.web.bind.annotation. It accepts a code (alias value) of type HttpStatus and an optional reason. It can be placed on:
201 Created);@ExceptionHandler method, so the handled exception maps to that status.Spring processes it through ResponseStatusExceptionResolver, which is part of the default exception resolvers in Spring MVC.
Annotate a custom exception and throw it from anywhere in the request path.
A request for a missing order now returns 404 Not Found. The exception message is not exposed in the response unless Spring Boot's error properties allow it (see server.error.include-message).
Use it when success has a specific status that is always the same.
If you also need a Location header, switch to ResponseEntity.created(uri) because the annotation cannot set headers.
For exceptions you do not own, such as IllegalArgumentException or library exceptions, you cannot annotate the class. Handle them in an advice instead and add the status to the handler.
ProblemDetail (Spring Framework 6 and later) follows the RFC 9457 problem details format and gives clients a consistent JSON error shape.
| Need | Better choice |
|---|---|
| Status depends on runtime data | ResponseEntity |
Status and headers such as Location | ResponseEntity |
| Throw an error with a status, without a custom class | ResponseStatusException |
| One consistent error body for the whole API | @RestControllerAdvice with ProblemDetail |
ResponseStatusException is often the quickest option for one-off cases and lets the status vary:
Two details to remember. First, the reason attribute makes Spring call response.sendError(...), which triggers the container error page and Spring Boot's /error handling, so prefer a ProblemDetail body for APIs. Second, annotating a domain exception with an HTTP status couples your domain layer to the web layer, so many teams keep the mapping in an advice instead.
It has no useful effect there. The status inside the ResponseEntity is used, so keep one mechanism per method.
It returns the annotated status with an empty body, which fits 204 No Content for deletes.
Yes. Boot uses Spring MVC's resolvers, and unhandled errors are also rendered by BasicErrorController at /error.
Use the annotation for fixed, reusable exception types, and ResponseStatusException when the status or message is decided inline.
@ResponseStatus links a fixed HTTP status to an exception class or handler method with almost no code. It works well for 404 style exceptions and 201 or 204 success responses. For dynamic statuses, headers or rich error bodies, use ResponseEntity, ResponseStatusException or a @RestControllerAdvice returning ProblemDetail.