The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
In Spring MVC, put an @ExceptionHandler in a controller for controller-specific behavior, or in @ControllerAdvice to handle exceptions across selected or all controllers. For REST APIs, @RestControllerAdvice makes handler return values response bodies. Use ProblemDetail when you want a standard RFC 9457 error representation, and extend ResponseEntityExceptionHandler when you need to customize Spring MVC’s built-in exception responses centrally.
This guide covers Servlet-based Spring MVC. WebFlux has a corresponding but distinct exception-handling model. Version-specific guidance here follows the Spring Framework 7.0.9 stable documentation; the 7.1.0-M2 documentation identifies that line as in development.
| # | Preview | Product | Price | |
|---|---|---|---|---|
| 1 |
|
Spring MVC: A Tutorial (Second Edition) | $44.99 | Buy on Amazon |
| 2 |
|
Spring MVC: Beginner's Guide - Second Edition | $50.99 | Buy on Amazon |
| 3 |
|
Spring Start Here: Learn what you need and learn it well | $49.99 | Buy on Amazon |
| 4 |
|
Spring MVC: Beginner's Guide | $50.99 | Buy on Amazon |
| 5 |
|
Expert Spring MVC and Web Flow (Expert's Voice in Java) | $32.36 | Buy on Amazon |
Choose where the handler belongs
| Approach | Scope | When it fits |
|---|---|---|
@ExceptionHandler in a controller |
That controller and its class hierarchy | Use when the exception response or recovery is specific to one controller. |
@ExceptionHandler in @ControllerAdvice |
Controllers included by the advice’s selectors; without narrowing, advice can apply broadly | Use for shared exception mappings, response conventions, or browser-facing error views. |
@ExceptionHandler in @RestControllerAdvice |
Controllers included by the advice’s selectors; without narrowing, advice can apply broadly | Use shared handling where return values should be written as response bodies. |
Advice can be narrowed by controller annotation, package, or assignable type. This lets an application share common error handling without forcing one policy onto every controller. A plain @ControllerAdvice can return a view as well as a response body; select @RestControllerAdvice when response-body behavior is the intended default.
How Spring MVC selects an exception handler
Spring can match an exception handler to the exception thrown or to a nested cause. Within a single controller or advice class, a match for the root exception is generally preferred over a cause match. Across advice beans, ordering can change the result: a cause match in higher-priority advice can take precedence over a root-exception match in lower-priority advice.
#1 Best Overall
Make matching predictable
- Use specific exception types in handler method arguments rather than broad exception types when different failures need different client-facing meanings.
- Keep related mappings together where practical, and explicitly order advice when multiple advice beans can handle overlapping exception hierarchies.
- When an unexpected method runs, inspect both the thrown exception and its cause chain, then check advice ordering and each handler’s declared exception type.
Official guidance on matching and advice scope is in the Spring MVC exception-handling reference and the Controller Advice reference.
Return an RFC 9457 problem response with ProblemDetail
Spring supports RFC 9457 problem details through ProblemDetail, ErrorResponse, and ErrorResponseException. An exception handler can return a ProblemDetail or ErrorResponse for Spring to render as an error response. The standard fields give clients a consistent structure; Spring also supports additional, non-standard properties through the ProblemDetail properties map.
The value of ProblemDetail.status determines the HTTP status. If instance is not set, Spring supplies the current URL path. For ProblemDetail, Spring’s JSON and XML message converters favor application/problem+json and application/problem+xml, respectively.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Example: one shared API mapping
@RestControllerAdvice
class ApiExceptionAdvice {
@ExceptionHandler(OrderNotFoundException.class)
ProblemDetail handleOrderNotFound(OrderNotFoundException ex) {
ProblemDetail problem = ProblemDetail.forStatus(HttpStatus.NOT_FOUND);
problem.setTitle("Order not found");
problem.setDetail(ex.getMessage());
return problem;
}
}
This example demonstrates the shape of a handler; adapt the detail exposed to clients to your application’s security and information-disclosure requirements. Spring’s Error Responses reference documents ProblemDetail behavior, but that URL is a 6.2 development snapshot, not a stable 7.0.9 reference. Check the stable reference for the Spring version you deploy before relying on version-specific details.
Return different errors for HTML and API clients
An exception handler may return a view for a browser request or a response body for an API request. When both representations are needed, declare distinct producible media types on handler methods and let content negotiation during error handling select the response appropriate to the client’s accepted media types.
Use this approach when the same failure has genuinely different representations, rather than creating duplicate mappings without a negotiation rule. The Spring MVC exceptions reference describes producible media types for exception handlers.
Rank #4
Customize Spring MVC’s built-in exception responses
ResponseEntityExceptionHandler is a base class for global @ControllerAdvice handling of Spring MVC exceptions with RFC 9457-formatted response details. Extend it when you need to customize framework-provided MVC error responses centrally; it provides per-exception and common response customization points. This is often less repetitive than recreating every built-in exception mapping yourself.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Use ordinary @ExceptionHandler methods for application-specific exceptions, and extend the base class when your goal is to tailor Spring MVC’s own exception responses. See the ResponseEntityExceptionHandler API documentation.
Quick Recap
Best Value
Put the pieces together
- Keep controller-only behavior in a local
@ExceptionHandler. - Move shared policies to
@ControllerAdviceor@RestControllerAdvice, and narrow advice with selectors if only some controllers should use it. - Prefer specific exception mappings and deliberate advice ordering to avoid surprising matches.
- Use
ProblemDetailorErrorResponsefor a standard API problem representation. - Extend
ResponseEntityExceptionHandlerto customize Spring’s built-in MVC exception responses. - Use producible media types and content negotiation when browser and API clients need different representations of the same failure.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

