Quick revision
The short answer
The starter contains a low-precedence @RestControllerAdvice that handles the standard Spring MVC exceptions and a catch-all, a base domain exception that carries a stable error code and HTTP status, and ProblemDetail output with shared extension fields such as code and traceId. Teams extend it by subclassing the base exception, by adding their own advices with higher precedence, or through small customizer hooks, and the starter logs consistently: client errors briefly at a low level, server errors at ERROR with stack traces.
What to remember
In this answer, we designed an error-handling starter. Auto-configuration brings a lowest-precedence advice extending ResponseEntityExceptionHandler, with handlers for a base ApiException that carries a code and status and a safe catch-all. Overriding handleExceptionInternal adds shared ProblemDetail fields and logging to every response, while security handlers and ErrorAttributes cover the edges. Teams extend it by subclassing the exception, by customizer hooks or by higher-precedence advices, and the starter logs client errors briefly and server errors with stack traces.
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.
The starter contains a low-precedence @RestControllerAdvice that handles the standard Spring MVC exceptions and a catch-all, a base domain exception that carries a stable error code and HTTP status, and ProblemDetail output with shared extension fields such as code and traceId. Teams extend it by subclassing the base exception, by adding their own advices with higher precedence, or through small customizer hooks, and the starter logs consistently: client errors briefly at a low level, server errors at ERROR with stack traces.
Why this question is asked
Every service needs the same error-handling code, and when every team writes it, we get many formats, duplicated handlers and inconsistent logs. A Spring Boot starter, a dependency that brings its own auto-configuration, turns the company standard into something a team adds in one line. Designing one well is harder than writing a single advice, because it must be correct by default and still leave teams room for their domain. Interviewers ask this design question to see whether we can balance a firm standard with clean extension points. In this answer, we will lay out what the starter contains, then build the base exception, the advice, the shared fields, the extension hooks and the logging rules.
1What the Starter Contains
A starter is a jar with an auto-configuration class listed in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. Spring Boot loads it when the jar is on the classpath, and conditions such as @ConditionalOnWebApplication and @ConditionalOnMissingBean decide which beans it registers, so a team can replace any piece by defining its own bean.
- Team codedomain exceptions extending
ApiException, optional own advices - Hooks
ProblemDetailCustomizerbeans, error code registry entries - Shared advicestandard Spring exceptions +
ApiException+ catch-all, lowest precedence - ProblemDetail factorycode,
traceId, errors list; same shape everywhere - EdgesSecurity 401/403 handlers, /error
ErrorAttributes, logging and metrics - Auto-configurationconditions, properties, opt-out switches
6 layers, from the top down. Step through them one at a time.
Teams work in the top layer; the starter guarantees the rest.
The pieces stack cleanly. Teams mostly touch the top layer, their own exception classes, and occasionally the hooks. Everything below works without any code from them.
2A Base Domain Exception With a Code and a Status
The heart of the extension model is one exception type that the starter understands. It carries a stable machine-readable code and the HTTP status, so the shared advice can map any subclass without knowing it.
The detail message of an ApiException is written by the team for clients, so it is safe to show. Exceptions that are not ApiException never have their messages exposed.
3The Shared Advice and Its Precedence
The advice extends Spring's ResponseEntityExceptionHandler, which already maps the standard Spring MVC exceptions, such as unreadable JSON, a wrong method or a missing parameter, to ProblemDetail responses. Because it extends that class, Spring Boot's own problem details advice backs off, so there is exactly one handler for those exceptions. We add a handler for ApiException and a catch-all, and give the advice the lowest precedence so any team advice runs first.
Notice that every response, whether from Spring's built-in handlers or ours, passes through handleExceptionInternal. Overriding that single method is how we guarantee the shared fields and the logging for all of them. The catch-all must also step aside for Spring Security's AccessDeniedException and AuthenticationException, rethrowing them so the security handlers produce 403 and 401.
4Shared Fields and the Edges Outside Spring MVC
Every ProblemDetail gets the same extension properties: code, a stable identifier from the company's error registry, traceId from Micrometer Tracing, and for validation an errors list of field, code and message. The type URI points to the developer portal page for that code.
The advice only sees exceptions inside DispatcherServlet, so the starter also covers the edges. It registers an AuthenticationEntryPoint and an AccessDeniedHandler that write the same format, which teams plug into their security configuration with one line, and an ErrorAttributes bean so that /error, which renders anything that escaped from filters, returns the same keys. A starter that covers only controllers would leave clients with two or three error shapes.
5How Teams Extend It
There are three extension routes, from simplest to most powerful. The first, enough for most cases, is subclassing ApiException: no handler needed. The second is a hook: a team defines a bean implementing ProblemDetailCustomizer, a small interface the starter calls for every response, to add a property such as orderId or tenant. The third is a team advice for exceptions it cannot change, such as one from a vendor library, annotated with a higher precedence, for example @Order(0), and documented in the starter's guide.
The starter also has switches: a property such as company.errors.enabled=false turns the auto-configuration off, and @ConditionalOnMissingBean lets a team replace the factory or the advice. Escape hatches keep teams from forking the library when they have a genuine special case.
6Consistent Logging by Status
The logging rule is part of the contract. Client errors, the 4xx statuses, are normal traffic: they are logged once at DEBUG or INFO with the code and the trace id, without a stack trace. Server errors, the 5xx statuses, are logged at ERROR with the full stack trace, because someone will need it. Each response also increments a counter tagged with the code and the status class, so dashboards can separate business errors from failures. Because this happens in the starter, a service does not need to remember any of it, and on-call engineers find the same log shape in every service.
7Follow-up questions
How do you version the starter without breaking clients?
Codes and the response shape are part of the API, so changing them is a major version. New fields and new codes are minor versions, because clients must ignore unknown fields. The starter is managed in the company BOM, and a compatibility test suite checks the output for each standard exception before every release.
What if a team needs a different format for one legacy client?
The team can add an advice with higher precedence, scoped with assignableTypes to the legacy controllers, that produces the old format. The rest of the service keeps the standard. That keeps the exception visible and local instead of a fork of the whole starter.
Why lowest precedence rather than highest?
Because the starter provides defaults and the team knows its domain better. With the starter at the lowest precedence, any team advice is consulted first, while the starter still catches everything the team did not handle. If the starter came first, its catch-all would swallow team exceptions.
8Conclusion
In this answer, we designed an error-handling starter. Auto-configuration brings a lowest-precedence advice extending ResponseEntityExceptionHandler, with handlers for a base ApiException that carries a code and status and a safe catch-all. Overriding handleExceptionInternal adds shared ProblemDetail fields and logging to every response, while security handlers and ErrorAttributes cover the edges. Teams extend it by subclassing the exception, by customizer hooks or by higher-precedence advices, and the starter logs client errors briefly and server errors with stack traces.
Open with
I'd build a Spring Boot starter with auto-configuration, so a team gets the standard by adding one dependency. At its core is a base exception, ApiException, that carries a stable error code and an HTTP status.
Then, if they want more
Teams subclass it for their domain, like OrderNotFoundException with order_not_found and 404, and they don't need to write any handler. The starter's @RestControllerAdvice extends ResponseEntityExceptionHandler, so it already handles all the standard Spring MVC exceptions with ProblemDetail, and Boot's own problem details handler backs off.
It adds handlers for ApiException and a catch-all that returns a generic 500 without leaking the message, and it rethrows Spring Security's exceptions. The advice has the lowest precedence, so team advices win.
I override handleExceptionInternal so every response, built-in or ours, gets the shared fields, code and traceId, plus a validation errors list, and goes through one logging rule: 4xx at a low level without stack traces, 5xx at ERROR with them, and a metric tagged by code.
For the edges, the starter ships security entry point and access denied handlers and an ErrorAttributes bean so /error has the same shape. Teams extend it with customizer beans or their own higher-precedence advice, and there's an off switch.
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 "Design a shared error-handling starter for all services. What would it contain and how...": https://scrivio-interview-library.netlify.app/spring-boot/error-handling/7170c8181e/