To handle exceptions in a Spring Boot REST API, create a @RestControllerAdvice class with @ExceptionHandler methods that translate each exception into an HTTP status and a consistent JSON body. On Spring Framework 6 and Spring Boot 3 the recommended body is ProblemDetail, which implements the standard RFC 9457 application/problem+json format.
This keeps controllers free of try/catch blocks, stops stack traces from leaking to clients, and gives every endpoint the same error shape.
@RestControllerAdvice combines @ControllerAdvice and @ResponseBody, so handler methods write their return value straight into the response. Start with a domain exception and a handler for it.
A request that throws ResourceNotFoundException now returns:
Spring fills in status and instance for you. You can add your own fields with problem.setProperty("code", "PRODUCT_NOT_FOUND").
Handlers you write cover your exceptions. For framework exceptions such as HttpRequestMethodNotSupportedException or HttpMessageNotReadableException, extend ResponseEntityExceptionHandler, which already maps them to ProblemDetail responses.
In Spring MVC you can also set spring.mvc.problemdetails.enabled=true in application.properties so Boot registers a default handler for the standard MVC exceptions. Pick one approach: if you extend ResponseEntityExceptionHandler yourself, you do not need the property.
Bean Validation failures from @Valid @RequestBody arrive as MethodArgumentNotValidException. Override the handler method from ResponseEntityExceptionHandler to return field-level detail.
The @Valid request DTO can be a record:
Since Spring 6.1, method-level validation of controller parameters can raise HandlerMethodValidationException instead, which ResponseEntityExceptionHandler also maps for you.
For simple cases you do not need a custom class. ResponseStatusException carries the status and reason directly.
Alternatively annotate an exception class with @ResponseStatus(HttpStatus.NOT_FOUND). Both are quick, but a central @RestControllerAdvice is easier to keep consistent and lets you log and add fields in one place.
Add a last-resort handler for unexpected exceptions. Log the details server-side and return a generic message.
Never return ex.getMessage() here, and keep server.error.include-stacktrace=never (the default in Boot). Spring picks the most specific matching @ExceptionHandler, so the catch-all only fires for exceptions you did not handle explicitly. Order several advice classes with @Order if you split them by area, and limit one with @RestControllerAdvice(basePackages = "com.example.api").
| Situation | Status |
|---|---|
| Invalid input, failed validation | 400 Bad Request |
| Missing or invalid credentials | 401 Unauthorized |
| Authenticated but not allowed | 403 Forbidden |
| Resource does not exist | 404 Not Found |
| State conflict, duplicate key | 409 Conflict |
| Unexpected server failure | 500 Internal Server Error |
@RestControllerAdvice is @ControllerAdvice plus @ResponseBody, so handler return values are serialised to JSON. Use it for REST APIs.
Prefer ProblemDetail on Spring 6 and Boot 3: it is a standard format, supported by clients and by the framework's own exceptions. A custom class is fine when you must keep an existing contract.
Not to @RestControllerAdvice, because filters run before the dispatcher servlet. Handle them in the filter or with Spring Security's AuthenticationEntryPoint and AccessDeniedHandler.
Use @WebMvcTest with MockMvc and assert the status and the JSON fields of the response.
Put all REST error handling in one @RestControllerAdvice, map domain exceptions to status codes, return ProblemDetail, and extend ResponseEntityExceptionHandler to cover validation and framework exceptions. Add a safe catch-all that logs internally and exposes nothing sensitive.