@ResponseStatus tells Spring MVC which HTTP status code to send. Placed on an exception class, it maps that exception to a status, so throwing ResourceNotFoundException produces a 404 without any handler code. Placed on a controller or @ExceptionHandler method, it sets the status of a successful response, for example 201 Created.
This answer concentrates on the exception use, which is the most common one. It is simple, but it has limits that make ResponseStatusException or a @RestControllerAdvice a better fit for APIs that return JSON error bodies.
| Target | Effect |
|---|---|
| Exception class | Any controller that throws it responds with the given status |
| Controller method | Sets the status for a normal return |
@ExceptionHandler method | Sets the status when that handler runs |
| Controller class (since Spring 4.2) | Default status for the class's methods |
The annotation has code and value (aliases of each other) for the status, and an optional reason text.
ResponseStatusExceptionResolver, one of the default exception resolvers in Spring MVC, finds the annotation on the thrown exception and sets the status. The request returns 404 instead of the 500 that an unhandled exception would produce.
The reason attribute looks like a way to send an error message, but it is implemented with HttpServletResponse.sendError(status, reason). That hands the request to the servlet container error handling, and in Spring Boot to BasicErrorController, so the body is Boot's standard error JSON (or the HTML error page), not a body you control. Boot also hides the message by default; the property server.error.include-message controls that. The reason text can be a message key that Spring resolves from a MessageSource for localisation.
Because of this, use reason sparingly. If you want a precise JSON body, return it yourself.
Spring 5 added ResponseStatusException, which carries a status without a dedicated exception class:
For a consistent JSON format across the whole API, handle exceptions in one place and return a ProblemDetail (RFC 9457):
Here the handler wins over the annotation on the exception, because @ExceptionHandler methods are consulted before ResponseStatusExceptionResolver.
| Approach | Custom body | Reusable | Good for |
|---|---|---|---|
@ResponseStatus on exception | No | Yes, per class | Small apps, quick status mapping |
ResponseStatusException | Message only | One-off | Single call sites |
@RestControllerAdvice | Full control | Yes, global | Production APIs |
@ExceptionHandler.reason set, the response goes through sendError, so the content type and body may differ from your normal JSON.Yes. The resolver looks at the annotation on whatever exception reaches it, whether checked or unchecked, as long as the controller method declares or wraps it.
The @ExceptionHandler runs first. The annotation is only used when no handler matches.
Not through reason. Return a body from an @ExceptionHandler or use ProblemDetail.
No. It is still supported in Spring MVC, although ResponseStatusException and @RestControllerAdvice are preferred for APIs.
Annotate an exception class with @ResponseStatus to map it to an HTTP status with almost no code. Avoid relying on reason for response bodies, and use ResponseStatusException or a @RestControllerAdvice returning ProblemDetail when you need consistent, structured error responses.