Quick revision
The short answer
I would agree on ProblemDetail (RFC 9457) as the single format and then fix each source that produces errors. Spring Security's AuthenticationEntryPoint and AccessDeniedHandler are configured to write ProblemDetail for 401 and 403, the gateway's own error responses such as 404 for no route, 429 and 502 or 504 are configured to the same format while it passes service errors through untouched, and /error responses are mapped through ErrorAttributes or a custom error controller. Then an end-to-end test triggers every error source and checks the shape.
What to remember
In this answer, we unified three error shapes by first finding every place an error is produced, then agreeing on ProblemDetail with shared fields. We configured Spring Security's AuthenticationEntryPoint and AccessDeniedHandler to write it, set the gateway's own error responses to the same shape while it passes service errors through, mapped /error through ErrorAttributes or a custom error controller, and protected the result with end-to-end tests that trigger every error source.
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 agree on ProblemDetail (RFC 9457) as the single format and then fix each source that produces errors. Spring Security's AuthenticationEntryPoint and AccessDeniedHandler are configured to write ProblemDetail for 401 and 403, the gateway's own error responses such as 404 for no route, 429 and 502 or 504 are configured to the same format while it passes service errors through untouched, and /error responses are mapped through ErrorAttributes or a custom error controller. Then an end-to-end test triggers every error source and checks the shape.
Why this question is asked
Clients see one API, but behind it three systems can answer with an error: the API gateway, the Spring Security filter chain in the service, and the service's own code. If each speaks its own format, every client needs three parsers, and errors like an expired token or a timed-out upstream end up shown as unknown error. Interviewers ask this scenario because the fix is not one clever handler; it is finding every place an error is produced and aligning each one. In this answer, we will map where the three shapes come from, agree on the format, and then fix Spring Security, the gateway and the /error path, finishing with how we test it end to end.
1Where the Three Shapes Come From
The first step is an inventory, because we cannot fix a source we have not found. We send deliberately bad requests and record what comes back: a request with no token, one with a token lacking a role, a path no route matches, a burst of requests over the rate limit, a call while the service is down, and a request with invalid JSON.
| Error | Produced by | Shape today |
|---|---|---|
| invalid JSON, domain errors | service advice | ProblemDetail |
| no or expired token (401) | Spring Security | empty body, WWW-Authenticate header |
| missing role (403) | Spring Security | empty body or Boot /error JSON |
| no route (404), rate limit (429) | gateway | gateway's own JSON |
| upstream down (502, 503, 504) | gateway | gateway's own JSON or HTML |
| exception in a servlet filter | Boot /error | timestamp, status, error, path |
Each shape is the correct default output of a different component. The service uses our advice, Spring Security writes its own minimal responses, and the gateway renders errors it produces itself with its own error handler. None of them is a bug; they were simply never told about each other.
2Agree on ProblemDetail as the Single Format
We pick RFC 9457 problem details, sent as application/problem+json, because Spring supports it natively and many gateways and client libraries understand it. The agreement covers the extension fields too: a stable code, a traceId, and an errors list for validation. Writing this down as a short shared schema, published in every OpenAPI spec, is what lets each team configure its component without guessing.
3Configure Spring Security to Write It
Spring Security handles authentication and authorisation failures in its own filter chain, before our controllers and their advice. Its ExceptionTranslationFilter calls an AuthenticationEntryPoint for 401 and an AccessDeniedHandler for 403. We provide implementations that write our format.
Notice that we keep the WWW-Authenticate header, which the HTTP standard requires on a 401, and that the body never says why a token failed, only that authentication is required. The resource server has its own entry point setting, so we configure it there; check your version for where each setting lives. ProblemWriter is a small shared helper that serialises a ProblemDetail with the trace id, so these responses match the advice exactly.
4Configure the Gateway the Same Way
The gateway produces its own errors when no route matches, when a rate limit is hit, when authentication at the edge fails, and when the upstream service times out or is unreachable. Every gateway product has a way to customise these: with Spring Cloud Gateway on WebFlux we provide our own ErrorAttributes or ErrorWebExceptionHandler bean, and commercial gateways usually offer response templates per status. We set each to the same ProblemDetail shape, with codes such as route_not_found, rate_limited with a Retry-After header, and upstream_timeout.
Equally important, the gateway must pass service error responses through unchanged. Some gateways rewrite upstream 5xx bodies by default, which would undo the work in the service. The gateway should also forward or create the trace context, so the traceId in a gateway-generated error can be found in the gateway's logs.
8 messages in order. Play them, or step through them one at a time.
5Map /error Responses and Test Every Source
Inside each service, some errors still escape Spring MVC, such as exceptions in servlet filters, and Spring Boot renders them at /error with ErrorAttributes. We customise ErrorAttributes or replace the error controller so /error returns the same ProblemDetail fields. The shared error starter can ship this, along with the Security helpers, so every service gets it at once.
Finally we test each source end to end, through the real gateway in a staging environment, not only with MockMvc inside one service. The suite sends the same deliberately bad requests as the inventory and asserts for each one the status, the content type application/problem+json, the presence of code and traceId, and the absence of stack traces. It runs on every gateway or starter change, because a gateway upgrade that changes its default error page would otherwise go unnoticed.
6Follow-up questions
Should the gateway translate service errors into its own format instead?
Only as a temporary bridge during migration. Translation at the gateway loses information, adds latency and logic to a shared component, and hides the problem from service teams. The lasting fix is for every component to emit the same format, with the gateway passing service bodies through.
What about clients that already parse the old shapes?
We announce the change, document the new format, and for a transition period support both, for example by honouring an Accept header or a version, prioritising external clients. Internal clients usually migrate quickly once the shared client library understands ProblemDetail.
Who owns the format once it is agreed?
The platform or API governance team owns the schema and the error code registry, and each component owner, gateway, security starter and services, owns implementing it. Clear ownership is what keeps a fourth shape from appearing next year.
7Conclusion
In this answer, we unified three error shapes by first finding every place an error is produced, then agreeing on ProblemDetail with shared fields. We configured Spring Security's AuthenticationEntryPoint and AccessDeniedHandler to write it, set the gateway's own error responses to the same shape while it passes service errors through, mapped /error through ErrorAttributes or a custom error controller, and protected the result with end-to-end tests that trigger every error source.
Open with
Three shapes mean three components each using its own default, so I'd treat it as finding and fixing every error source rather than writing one handler. First I'd do an inventory with deliberately bad requests: no token, wrong role, unknown route, rate limit, service down, bad JSON, and a filter error, and record who answers and in what shape.
Then, if they want more
Then we agree on ProblemDetail as the single format, with shared extensions like code and traceId and an errors list for validation, published as a schema in the OpenAPI specs.
In the services, Spring Security answers 401 and 403 before our advice runs, so I configure its AuthenticationEntryPoint, including the one on the resource server, and its AccessDeniedHandler to write ProblemDetail through a shared helper, keeping the WWW-Authenticate header.
At the gateway I configure its own errors, no route, rate limiting with Retry-After, upstream timeouts, to the same shape, and make sure it passes service error bodies through unchanged and propagates the trace context. Inside services I map /error through ErrorAttributes or a custom error controller so filter errors match too.
I'd ship the security and /error parts in the shared starter. Finally, an end-to-end suite through the real gateway triggers every source and checks status, content type and fields.
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 API gateway, Spring Security and services all produce error responses in different...": https://scrivio-interview-library.netlify.app/spring-boot/error-handling/7b1d42e109/