In Servlet-based Spring MVC, use a controller’s @ExceptionHandler for errors specific to that controller, and use @ControllerAdvice or @RestControllerAdvice for shared handling. Return ProblemDetail or ErrorResponse when you want an RFC 9457 problem response. The key to predictable behavior is to keep exception mappings specific and account for both nested-cause matches and advice priority.
This guidance follows the stable Spring Framework 7.0.9 documentation context. Spring MVC and WebFlux have different execution models; the examples and behavior here concern Spring MVC.
| # | 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.51 | Buy on Amazon |
As an Amazon Associate I earn from qualifying purchases.
Choose where the handler belongs
Spring MVC checks for exception handlers in the controller where an exception occurred, then can use applicable advice beans for handling shared across controllers. Scope is the first design choice: keep a mapping local when its meaning belongs to one controller, and move it to advice when multiple controllers should return a consistent response.
Controller-local handling
Put an @ExceptionHandler method in a controller when it handles an exception tied to that controller’s behavior. The handler applies to that controller and its class hierarchy, not automatically to every controller in the application.
#1 Best Overall
Shared handling with advice
Use @ControllerAdvice for cross-controller handling. It can be restricted to selected controllers by annotation, package, or assignable type, so a shared handler need not apply to the whole application. @RestControllerAdvice adds response-body behavior, making it a natural choice when handlers should serialize response data for an API. In applications that serve HTML as well as APIs, exception handlers can instead return a view or a body response as appropriate.
How Spring selects an exception handler
Spring MVC can match either the exception thrown by the controller or a nested cause. A mapping for a broad exception type can therefore catch more than the top-level exception visible at the call site. Prefer handler arguments that name the specific exception types whose client-facing meaning you intend to handle.
Root and cause matches
Within a single controller or advice class, Spring generally favors a match to the root exception over a match to a nested cause. Across advice beans, ordering can change the result: a cause match in higher-priority advice can beat a root match in lower-priority advice. An unexpected handler may therefore reflect advice order, not just the exception class named in the method.
PC 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 & 11Crashes, 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 minuteMake matching deliberate
- Use separate, specific mappings when different exception types require different client-facing status codes or messages.
- Review both method signatures and advice ordering when a handler that appears less specific is selected.
- Use scoped advice when different controller groups need distinct error behavior.
Spring’s Spring MVC exception handling reference describes handler matching, including nested causes and producible media types. For advice scope and selectors, see the Controller Advice reference.
Return a consistent API error with ProblemDetail
Spring Framework supports RFC 9457 problem details through ProblemDetail, ErrorResponse, and ErrorResponseException. An exception handler can return ProblemDetail or ErrorResponse to produce a structured problem response instead of an application-specific error shape.
Set ProblemDetail.status to the HTTP status the client should receive. If instance is unset, Spring supplies the current URL path. The standard problem fields provide a common structure, and Spring also supports additional properties through the ProblemDetail properties map. These properties are useful for application-specific details, but avoid exposing internal exception text or implementation details to clients.
Rank #4
Spring’s JSON and XML converters favor application/problem+json and application/problem+xml when rendering ProblemDetail. Consult the Spring error responses reference for the framework’s RFC 9457 support; that linked page is a 6.2 development snapshot, so check the matching stable reference for version-specific details when using another release.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →Customize Spring MVC’s built-in error responses
If your goal is to customize Spring’s existing MVC exception responses centrally, consider extending ResponseEntityExceptionHandler in a global @ControllerAdvice. It is designed as a base class for RFC 9457-formatted responses to Spring MVC exceptions and provides per-exception and common customization points. This is often a better fit than recreating each built-in mapping yourself.
Best Value
See the ResponseEntityExceptionHandler API documentation for the available customization methods.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Serve HTML and JSON according to the request
If browser requests should receive an HTML error view while API clients receive JSON, declare distinct producible media types on the relevant exception handler methods. Spring can then use content negotiation during error handling to choose a representation suited to the request. This keeps the exception mapping tied to the same error case while allowing different response formats.
The Spring MVC exception handling reference documents producible media types for exception handlers and how they participate in content negotiation.
Free tools Windows power users keep installed
One-click scans. No signup required.
Quick Recap
Choose an implementation pattern
| Pattern | Scope | Response approach | Best fit |
|---|---|---|---|
Controller @ExceptionHandler |
One controller and its class hierarchy | View or response body | Controller-specific error behavior |
@ControllerAdvice |
All controllers or selected controllers | View or response body | Shared handling, including apps serving HTML |
@RestControllerAdvice |
All controllers or selected controllers | Response body | Shared API error responses |
@RestControllerAdvice with ProblemDetail or ErrorResponse |
All controllers or selected controllers | RFC 9457 problem response | Consistent, structured API errors |
@ControllerAdvice extending ResponseEntityExceptionHandler |
Shared Spring MVC exception handling | Customizable RFC 9457 responses | Central customization of built-in MVC exception responses |
Check these points when a handler behaves unexpectedly
- Is the handler in the right scope? A controller-local handler does not automatically cover other controllers. Use advice for shared behavior, with selectors if only some controllers should be included.
- Does the mapping match a cause? Spring can match nested causes as well as the top-level exception.
- Is advice priority affecting selection? A cause match in higher-priority advice can outrank a root match in lower-priority advice.
- Does the response carry the intended status? For a
ProblemDetail, set itsstatusdeliberately. - Should the client receive a view or a body? Choose
@ControllerAdviceor@RestControllerAdviceand handler return types to fit the application’s clients. - Should content negotiation choose the format? Declare producible media types when the same error needs different HTML and JSON responses.
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.




