Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

Define an error-handling standard for a portfolio of JAX-RS and Spring MVC services so clients see the same error format, codes and correlation ids regardless of framework.

I would adopt RFC 9457 problem details (application/problem+json) as the single error format, with stable type URIs and a machine-readable code extension, and ship two small shared libraries that produce exactly that shape: JAX-RS ExceptionMappers and Spring @ControllerAdvice built on ProblemDetail. Validation, not found, conflict, authentication, authorization and rate-limit errors map to fixed statuses and codes, internals such as stack traces and SQL never leak, and every error carries the W3C trace id so support can find the logs. A cross-framework contract test suite and an error catalog in the API documentation keep every service honest.

Why this question is asked

Clients of a large API portfolio do not care which framework a service uses; they care that a 404 looks the same everywhere and that an error code tells them what to do. Without a standard, each service invents its own error JSON, client code fills with special cases, and support teams cannot match a customer's error to a log line. Interviewers ask this design question to see whether we can define a standard that works across frameworks and make it stick. In this answer, we will pick the format, define the shape and the mapping of error categories, build the shared libraries for JAX-RS and Spring, add trace ids, and verify everything with a contract suite and an error catalog.

1Choosing the Format: RFC 9457 Problem Details

RFC 9457, which replaced RFC 7807 in 2023, defines problem details: a JSON object served with the media type application/problem+json. Its standard members are type, a URI that identifies the kind of problem, title, a short summary that does not change between occurrences, status, the HTTP status code, detail, an explanation of this occurrence, and instance, a URI for this occurrence. It explicitly allows extension members for anything else we need.

Using a published standard instead of inventing a format has practical benefits. Spring Framework 6 and later supports it natively with the ProblemDetail class, client libraries and API gateways understand it, and new teams can read the RFC instead of a company wiki page.

2Defining the Shape and the Categories

We add three extension members. code is a stable, machine-readable error code such as ORDER_NOT_FOUND, which clients switch on instead of parsing titles. traceId links the response to the logs. errors is a list of field problems for validation failures. The type URI points to a page in the error catalog that explains the problem and what to do about it.

YAML
HTTP/1.1 422 Unprocessable Content
Content-Type: application/problem+json
{
"type": "https://api.example.com/problems/validation-failed",
"title": "Request validation failed",
"status": 422,
"detail": "2 fields are invalid.",
"instance": "/orders",
"code": "VALIDATION_FAILED",
"traceId": "4bf92f3577b34da6a3ce929d0e0e4736",
"errors": [
{"field": "quantity", "message": "must be at least 1"},
{"field": "currency", "message": "must be a 3-letter ISO code"}
]
}
Error categories and their mapping
CategoryStatusExample codeNotes
Validation400 or 422VALIDATION_FAILEDerrors[] lists fields; pick one status company-wide
Not found404ORDER_NOT_FOUNDAlso for resources the caller may not see
Conflict409ORDER_ALREADY_SHIPPEDState does not allow the action
Not authenticated401AUTH_REQUIREDWith WWW-Authenticate header
Not authorized403FORBIDDENNo detail about why
Rate limited429RATE_LIMITEDWith Retry-After header
Unexpected500INTERNAL_ERRORGeneric detail, traceId only

Then we fix how each category maps to a status and a code, so the same situation looks the same in every service, whatever framework raised it.

3Shared Libraries for Both Frameworks

Teams should not write this by hand. The platform team ships two libraries that produce identical JSON. For JAX-RS, a feature registers ExceptionMappers for the company's base exceptions, for WebApplicationException (preserving the runtime's 404, 405 and 415) and for validation errors, plus a catch-all. For Spring MVC, a @RestControllerAdvice extends ResponseEntityExceptionHandler, which already produces ProblemDetail for Spring's own exceptions, and adds handlers for the company exceptions. Both share one core module with the exception classes, the codes and the problem builder.

Java
// Shared core: framework-free
public class ApiException extends RuntimeException {
public ApiException(ErrorCode code, String detail) { super(detail); this.code = code; }
public final ErrorCode code; // ORDER_NOT_FOUND -> 404, type URI, title
}
// Spring MVC adapter
@RestControllerAdvice
class ApiErrors extends ResponseEntityExceptionHandler {
@ExceptionHandler(ApiException.class)
ProblemDetail handle(ApiException e) {
ProblemDetail p = ProblemDetail.forStatusAndDetail(HttpStatus.valueOf(e.code.status()), e.getMessage());
p.setType(e.code.typeUri());
p.setTitle(e.code.title());
p.setProperty("code", e.code.name());
p.setProperty("traceId", Traces.currentTraceId());
return p; // application/problem+json
}
}
// JAX-RS adapter
@Provider
public class ApiExceptionMapper implements ExceptionMapper<ApiException> {
public Response toResponse(ApiException e) {
return Problems.response(e.code, e.getMessage(), Traces.currentTraceId()); // same JSON
}
}

Notice that services throw new ApiException(ErrorCode.ORDER_NOT_FOUND, ...) in both frameworks. The adapters are thin, so the shape cannot drift between them. Security matters here too: the catch-all in each adapter returns only a generic message and the trace id, never stack traces, exception class names or SQL text, which attackers use to learn about the system.

4Propagating the Trace Id

Support can only help a customer if the error leads to the logs. W3C Trace Context defines the traceparent header, which carries a 32-hex-character trace id across services. With OpenTelemetry or Micrometer Tracing in every service, each incoming request joins or starts a trace, and the id appears in every log line through the logging context.

The shared libraries read the current trace id and put it in the problem body as traceId, and often in a response header too. So when a customer reports "422 with traceId 4bf92f35...", support searches the logs for that id and sees the whole request across every service it touched.

Step throughThe error standard, layer by layer
  1. Contract: RFC 9457 shape, codes, type URIsPublished in the error catalog
  2. Shared core libraryApiException, ErrorCode, problem builder
  3. Framework adaptersJAX-RS mappers and Spring @RestControllerAdvice
  4. Tracingtraceparent propagated, traceId in every body
  5. VerificationCross-framework contract test suite

5 layers, from the top down. Step through them one at a time.

5Verifying It Across Frameworks

A standard without verification decays. The contract test suite is a set of scenarios every service runs against itself in CI: call an unknown path and expect a 404 problem, send an invalid body and expect the validation shape, send no token and expect 401 with WWW-Authenticate, trigger the rate limit in a test profile and expect 429 with Retry-After, and force an unexpected error and check that the body has only the generic message and a trace id. Each response is validated against one JSON Schema for the problem shape.

The error catalog in the API documentation lists every code with its status, meaning and what the client should do, and each type URI resolves to its catalog page. New codes are added through API review, so the catalog stays the single source of truth.

6Follow-up questions

Should validation errors be 400 or 422?

Both are used. 400 means the request is malformed in general, and 422 means it is well-formed but semantically invalid. What matters is choosing one for the whole portfolio and documenting it, so clients do not need per-service logic.

How do you handle errors from the API gateway itself?

Configure the gateway to return the same problem format for its own errors, such as 401 from token checks or 429 from rate limits, and to pass through service errors unchanged. Otherwise clients see two formats depending on where the request failed.

How do you migrate existing services with their own error format?

Treat it as an API change. Add the new format for new endpoints immediately, offer it for existing ones behind content negotiation or a new API version, and retire the old format on a published date after consumers have moved.

7Conclusion

In this answer, we saw that a cross-framework error standard starts with RFC 9457 problem details plus a stable code, a trace id and field errors, and a fixed mapping of categories such as validation, not found, conflict, authentication and rate limiting to statuses and codes. Shared JAX-RS and Spring libraries built on one core produce the same shape without leaking internals, W3C trace context links each error to its logs, and a contract test suite with an error catalog keeps every service consistent.

What to say in the interviewAbout 90 seconds aloud

Open with

I would base the standard on RFC 9457 problem details, application/problem+json, because it is a published format that Spring supports natively with ProblemDetail and that clients and gateways already understand.

Then, if they want more

Every error has type, title, status, detail and instance, plus three extensions: a stable machine-readable code like ORDER_NOT_FOUND that clients switch on, a traceId, and an errors list for validation failures. Type URIs point to pages in an error catalog.

Then I would fix the mapping of categories: validation to one chosen status, 400 or 422, not found to 404, conflicts to 409, missing authentication to 401 with WWW-Authenticate, forbidden to 403, rate limiting to 429 with Retry-After, and unexpected errors to a generic 500 that never contains stack traces, class names or SQL.

To make it real, the platform team ships a shared core with an ApiException and the error codes, and two thin adapters: JAX-RS ExceptionMappers, including one for WebApplicationException so runtime 404s and 405s keep their status, and a Spring @RestControllerAdvice extending ResponseEntityExceptionHandler. Both produce identical JSON.

For support, every service propagates the W3C traceparent header through OpenTelemetry or Micrometer Tracing and puts the trace id in the body. Finally, a contract test suite runs the same error scenarios against every service and validates responses against one JSON Schema, and new codes go through API review into the catalog.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 6
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
Jakarta EE specifications
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 "Define an error-handling standard for a portfolio of JAX-RS and Spring MVC services so...": https://scrivio-interview-library.netlify.app/java/jax-rs/00546dee1f/