Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

Spring MVC Exception Handling: @ExceptionHandler, @ControllerAdvice, and ProblemDetail

A practical guide to controller-local @ExceptionHandler methods, shared @ControllerAdvice, Spring’s exception matching order, and RFC 9457 ProblemDetail responses.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Make 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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Quick Recap

Bestseller No. 4
SaleBestseller No. 5

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 its status deliberately.
  • Should the client receive a view or a body? Choose @ControllerAdvice or @RestControllerAdvice and 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.