Quick revision
The short answer
I would put the decision of what an exception means in a stack-independent core module that maps exceptions to a ProblemDetail, and keep each web stack's part as a thin adapter: a @RestControllerAdvice for Spring MVC, and for WebFlux a @RestControllerAdvice for annotated controllers plus an ErrorWebExceptionHandler for functional routes and everything outside controllers. Security failures need their own adapters, because AuthenticationEntryPoint and AccessDeniedHandler (servlet) and their reactive counterparts run before any controller advice. One shared set of contract tests runs against both adapters.
What to remember
In this answer, we saw that Spring MVC and WebFlux handle errors in different places: resolvers and /error in MVC, controller advice plus WebExceptionHandler in WebFlux, and security filters in both. A consistent library puts the meaning of each exception in a core ProblemMapper with no web dependencies, adds a thin adapter per stack, covers Spring Security's entry points and access denied handlers, which bypass advice, and proves the result with one shared set of contract tests run through MockMvc and WebTestClient.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would put the decision of what an exception means in a stack-independent core module that maps exceptions to a ProblemDetail, and keep each web stack's part as a thin adapter: a @RestControllerAdvice for Spring MVC, and for WebFlux a @RestControllerAdvice for annotated controllers plus an ErrorWebExceptionHandler for functional routes and everything outside controllers. Security failures need their own adapters, because AuthenticationEntryPoint and AccessDeniedHandler (servlet) and their reactive counterparts run before any controller advice. One shared set of contract tests runs against both adapters.
Why this question is asked
A company that runs both Spring MVC and WebFlux services usually ends up with two error formats, because the two stacks handle errors through different machinery. Clients notice: one service returns a problem detail with an errors list, another returns Boot's default JSON, and a 401 from the gateway looks different again. Interviewers ask this design question to see whether we know where each stack handles errors and whether we can share the important part without pretending the stacks are the same. In this answer, we will recap the error paths in each stack, design the shared core, build the two adapters, deal with security, and test the whole thing once for both.
1How Each Stack Handles Errors
Spring MVC, the servlet stack, handles exceptions from controllers with HandlerExceptionResolvers, which include the one that calls @ExceptionHandler methods in @ControllerAdvice classes. Anything left over goes to the servlet container and Spring Boot's /error endpoint. WebFlux, the reactive stack, also supports @ControllerAdvice, but only for annotated controllers. Functional routes defined with RouterFunction, and errors that happen outside a handler, go through WebExceptionHandler beans instead. Spring Boot registers one called DefaultErrorWebExceptionHandler, an ErrorWebExceptionHandler, which plays the role of /error.
Both stacks share some useful types from spring-web: ProblemDetail, ErrorResponse, ResponseStatusException and HttpStatusCode. That shared vocabulary is what makes a common library possible.
| Error source | Spring MVC | WebFlux |
|---|---|---|
| Annotated controller | @ControllerAdvice via resolver | @ControllerAdvice |
| Functional route | router onError(); advice support varies by version | router onError() or ErrorWebExceptionHandler |
| Framework (404, 405, 415) | ResponseEntityExceptionHandler | reactive ResponseEntityExceptionHandler |
Filter or WebFilter | container, then /error | ErrorWebExceptionHandler |
| Authentication failure | AuthenticationEntryPoint | ServerAuthenticationEntryPoint |
| Access denied | AccessDeniedHandler | ServerAccessDeniedHandler |
2The Shared Core: One Mapping, No Web Stack
The core module depends only on spring-web for ProblemDetail and on our own exception types. Its job is to answer one question: given this throwable, what status, type, title and extra properties should the client see? It knows our domain exception hierarchy, the validation exceptions, and the rule that anything unknown is a 500 without internal details. It also adds the fields every service should return, such as a trace id taken from the current observation.
Notice the ValidationFailure type. Field errors arrive in different exception classes in each stack (MethodArgumentNotValidException in MVC, WebExchangeBindException in WebFlux, HandlerMethodValidationException in both), so each adapter converts them into a neutral list of field errors first. The core then formats them one way. Keeping this conversion at the edge is what lets the core stay free of servlet and reactive types.
3The Two Adapters
Each adapter is a few dozen lines and lives in a stack-specific auto-configuration, guarded by @ConditionalOnWebApplication(type = SERVLET) or (type = REACTIVE), so a service gets the right one just by adding the starter. The MVC adapter is a @RestControllerAdvice extending the servlet ResponseEntityExceptionHandler. The WebFlux adapter is a @RestControllerAdvice extending the reactive ResponseEntityExceptionHandler (a different class in org.springframework.web.reactive.result.method.annotation), plus an ErrorWebExceptionHandler bean ordered before Boot's default one, so functional routes and WebFilter errors get the same body.
Notice the committed check. In a streaming response, part of the body may already be on the wire when an error happens, and then no handler can change the status. Both adapters must handle that case by passing the error on rather than writing a second response.
4Security Exceptions Bypass Controller Advice
Spring Security runs in a filter chain (servlet) or a WebFilter chain (reactive), before any controller is chosen. When authentication is missing, it calls an AuthenticationEntryPoint; when the user is authenticated but not allowed, it calls an AccessDeniedHandler. The reactive equivalents are ServerAuthenticationEntryPoint and ServerAccessDeniedHandler. None of these goes through @ControllerAdvice, so a library that only ships advice leaves 401 and 403 responses in a different format.
So the starter also provides implementations of all four that call the same ProblemMapper with an AuthenticationException or AccessDeniedException, and documents a one-line customizer that teams apply in their SecurityFilterChain or SecurityWebFilterChain. Method security, such as @PreAuthorize, throws inside the controller call, so there the advice does see the exception, and the core maps it to the same 403.
- Service codethrows domain exceptions from the shared hierarchy
- MVC adapteradvice, security entry point and access denied handler
- WebFlux adapteradvice,
ErrorWebExceptionHandler, reactive security handlers - Core ProblemMapperexception ->
ProblemDetail, trace id, no web stack - Contract test kitone scenario list run against both adapters
5 layers, from the top down. Step through them one at a time.
Only the adapters know about servlets or reactive types.
5One Set of Contract Tests for Both Stacks
The promise of the library is that the same failure looks the same everywhere, so we test exactly that. The test kit defines scenarios once, such as validation failure, unknown resource, wrong method, unauthenticated, forbidden, unexpected exception, each with the expected status and JSON shape. An abstract test runs them through MockMvc for an MVC sample application and through WebTestClient for a WebFlux sample, including a functional route. Services can reuse the same kit in their own builds to prove they did not override the library by accident.
We also version the library carefully. A change in the error format is an API change for every client of every service, so it goes through the same review as any contract change, and the contract tests make the change visible in one diff.
6Follow-up questions
Why not only use @ControllerAdvice in WebFlux?
Because it only covers annotated controllers. Functional endpoints and errors raised in WebFilters never reach it. Spring Boot's ErrorWebExceptionHandler handles those, and if we do not replace or customise it, those errors come back in Boot's default format instead of ours.
Should the library replace Spring Boot's error handling completely?
No. It should take over the body format and keep Boot's behaviour where it works, such as the error dispatch to /error in MVC for errors from filters. In MVC we can provide our own ErrorAttributes or an ErrorController that calls the same mapper, so even container-level errors match. Replacing more than needed makes upgrades harder.
How do we handle errors in a streaming WebFlux response?
If the status and headers are already committed, we cannot send a problem detail. We log the error with its trace id and terminate the stream; for server-sent events, we can send a final error event in our own format. The adapter checks isCommitted and passes the error on instead of trying to write a second response.
7Conclusion
In this answer, we saw that Spring MVC and WebFlux handle errors in different places: resolvers and /error in MVC, controller advice plus WebExceptionHandler in WebFlux, and security filters in both. A consistent library puts the meaning of each exception in a core ProblemMapper with no web dependencies, adds a thin adapter per stack, covers Spring Security's entry points and access denied handlers, which bypass advice, and proves the result with one shared set of contract tests run through MockMvc and WebTestClient.
Open with
I would separate what an error means from how each stack delivers it. The core module is a ProblemMapper: it takes a throwable and returns a ProblemDetail with status, type, title, field errors and a trace id, and it knows nothing about servlets or reactive types.
Then, if they want more
On top of it I put two thin adapters, auto-configured by web application type. For Spring MVC, a @RestControllerAdvice that extends ResponseEntityExceptionHandler and calls the mapper. For WebFlux, a @RestControllerAdvice for annotated controllers, and an ErrorWebExceptionHandler ordered before Boot's default one, because functional routes and WebFilter errors never reach controller advice.
Each adapter first converts its stack's validation exceptions into a neutral list of field errors, so the core formats them one way. Then security: Spring Security handles 401 and 403 in filters, so advice never sees them. The library ships an AuthenticationEntryPoint and AccessDeniedHandler for servlet apps and the Server variants for reactive apps, all calling the same mapper.
Finally, I write the expected behaviour once as scenarios, validation, not found, wrong method, unauthenticated, forbidden, unexpected, and run them with MockMvc against an MVC sample and with WebTestClient against a WebFlux sample, including a functional route. Services can run the same kit to prove nothing overrides the library.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Your services are split between Spring MVC and WebFlux, and the error handling...": https://scrivio-interview-library.netlify.app/spring-boot/error-handling/8ce11bbe3f/