Skip to content

    System Design

    The Error Model: RFC 9457 Problem Details

    Give every API failure one shape with RFC 9457 problem details: type, status, title, detail, instance and extensions, built in Spring and checked with MockMvc.

    By Published 8 min read

    Share

    Focused lessonbeginnerSpring Boot labBackend System Design

    Before this: API Design

    What you'll learn: By the end of this lesson you will be able to name the five standard members of an RFC 9457 problem details object, return one from a Spring application for validation failures, missing resources, business conflicts and unexpected exceptions, add extension members that a client can use without parsing prose, and keep internal detail out of the response.

    A parcel carrier leaves the same slip whatever went wrong: a code in a box, a short reason, a line for this delivery, and a reference number. You can sort a thousand slips without reading them. An API that fails in a different shape on every endpoint is a driver who leaves a different sticky note each time.

    This lesson is the error half of API Design. Bad input is in Input Validation and Injection Prevention and tracing a failure across services in Correlation IDs and Trace Context.

    Verified with RFC 9457, RFC 9110 and the Spring Framework reference (the 7.1.0 page; the lab runs Spring Framework 7.0.10 from Spring Boot 4.1.2), on 2026-12-01.

    The core idea

    A problem details object is a small JSON document with a fixed set of members that says what kind of failure happened, so a client can branch on the kind and a person can read the rest. RFC 9457 defines it, and its media type is application/problem+json. The status code carries the broad class; the object adds the specific kind and the facts of this occurrence.

    Three illustrations run through the lesson: a minimal example (the lab), a realistic example (a checkout client) and a failure example (an exception message that leaks an internal host name).

    The members of a problem

    RFC 9457 defines five standard members, all optional:

    Member Meaning in the RFC Use it for
    type A URI reference that identifies the problem type; when absent it is assumed to be about:blank The key a client branches on
    status The HTTP status code of this occurrence; advisory, and the response must carry the same code Readers that only see the body
    title A short, human-readable summary of the problem type A stable label
    detail A human-readable explanation specific to this occurrence The sentence for a person
    instance A URI reference that identifies this occurrence A pointer for support and logs

    Two rules do most of the work. Generators "MUST use the same status code in the actual HTTP response", so generic HTTP software keeps working. And consumers SHOULD NOT parse the detail member for information: anything a program needs goes into an extension member, and clients "MUST ignore any such extensions that they don't recognize". The type URI may be non-resolvable; if it does resolve, it should lead to human-readable documentation.

    Where a problem object is made in a Spring requestClient to Controller (request); Controller to Error advice (exception); Error advice to Client (problem); Error advice to Log (message and stack)Client: reads problem+jsonClientreadsproblem+jsonController: throws an exceptionControllerthrows anexceptionError advice: maps it to a problemError advicemaps it to aproblemLog: keeps the real causeLogkeeps the realcauserequestexceptionproblemmessage and stack

    Figure 1. One place turns exceptions into problems. How to read this: controllers throw; a single advice class decides what the caller sees, and the full cause goes to the log, not the response.

    How Spring produces them

    Spring Framework supports RFC 9457 directly. ProblemDetail is "a simple container for both standard fields defined in the spec, and for non-standard ones". Every Spring MVC exception implements ErrorResponse, and extending ResponseEntityExceptionHandler in an @ControllerAdvice renders all of them, and any ErrorResponseException, as problem details. The Jackson codecs use application/problem+json as the producible media type, so it wins content negotiation. In Spring Boot, the property spring.mvc.problemdetails.enabled registers such a handler for you; your own advice must be ordered ahead of it.

    Extension members go into the object's properties map, which Jackson renders as top-level JSON members.

    flowchart TD accTitle: Choosing the members of a problem accDescr: A decision flow. If the failure is one a client should branch on, give it a type URI and an extension for any data the program needs. If it is only a generic HTTP condition, leave type as about:blank and use the status. Either way the status in the body matches the response status and detail is for people. A["A request failed"] --> B{"Will a client branch on
    this specific kind?"} B -->|"yes"| C["Own type URI
    plus extensions for data"] B -->|"no, a generic HTTP condition"| D["type about:blank
    status says it all"] C --> E["status = response status
    detail = sentence for a person"] D --> E

    Figure 2. Deciding how much to say. How to read this: a type URI is a promise to clients, so give one only to failures worth a promise.

    Build it: one orders API, five kinds of failure

    The lab is a tiny ShopStream orders API. A controller throws domain exceptions; ProblemAdvice extends ResponseEntityExceptionHandler. MockMvc sends the requests in memory, so this shows what the server answers; there is no servlet container, no network and no real client.

    Run it from examples/spring-labs:

    cd examples/spring-labs && ./gradlew :labs:problem-details:test

    Nine tests assert the behaviour below and one compares the whole report with examples/spring-labs/golden/problem-details.txt.

    Expected output, captured from a real run (Java 25 language level, Spring Framework 7.0.10, Spring Boot 4.1.2, 2026-12-01), scenarios 1 to 3:

    == No error advice: Spring MVC's built-in resolvers
    --- 1. A body that fails validation
        request     : POST /v1/orders
        status      : 400
        Content-Type: (none)
        body        : (empty)
    == One error model: ProblemAdvice extends ResponseEntityExceptionHandler
    --- 2. A success, for contrast
        request     : GET /v1/orders/42
        status      : 200
        Content-Type: application/json
        body        : {"id":42,"status":"SHIPPED"}
    --- 3. A domain exception: unknown order
        request     : GET /v1/orders/7
        status      : 404
        Content-Type: application/problem+json
        body        : {"detail":"No order with id 7 exists.","instance":"/v1/orders/7","status":404,"title":"Order not found","type":"https://shopstream.example/problems/order-not-found","orderId":7}

    Without advice the refusal is a bare 400: no body, no content type. The missing order carries all five members and an extension, orderId. The type is an illustrative URI on shopstream.example, not a live page.

    Expected output, captured from a real run (same run), scenarios 4 and 5:

    --- 4. A body that fails validation
        request     : POST /v1/orders
        status      : 400
        Content-Type: application/problem+json
        body        : {"detail":"Invalid request content.","instance":"/v1/orders","status":400,"title":"Request body failed validation","type":"https://shopstream.example/problems/validation-failed","errors":[{"pointer":"/quantity","constraint":"Min"},{"pointer":"/sku","constraint":"NotBlank"}]}
    --- 5. A body that is not JSON
        request     : POST /v1/orders
        status      : 400
        Content-Type: application/problem+json
        body        : {"detail":"Failed to read request","instance":"/v1/orders","status":400,"title":"Bad Request"}

    Spring's default detail for a failed body is "Invalid request content." The advice adds an errors extension with one entry per failed field. RFC 9457's own validation example uses an errors member with a pointer and calls it fictional: the member is yours to define and document with the problem type.

    Statuses, Allow and conflicts

    Expected output, captured from a real run (same run), scenarios 6 to 8:

    --- 6. A path variable of the wrong type
        request     : GET /v1/orders/abc
        status      : 400
        Content-Type: application/problem+json
        body        : {"detail":"Failed to convert 'id' with value: 'abc'","instance":"/v1/orders/abc","status":400,"title":"Bad Request"}
    --- 7. A method the path does not support
        request     : DELETE /v1/orders/42
        status      : 405
        Content-Type: application/problem+json
        Allow       : GET
        body        : {"detail":"Method 'DELETE' is not supported.","instance":"/v1/orders/42","status":405,"title":"Method Not Allowed"}
    --- 8. A business conflict: cancelling a shipped order
        request     : POST /v1/orders/42/cancel
        status      : 409
        Content-Type: application/problem+json
        body        : {"detail":"Order 42 has already shipped and can no longer be cancelled.","instance":"/v1/orders/42/cancel","status":409,"title":"Order cannot be cancelled","type":"https://shopstream.example/problems/order-already-shipped","orderId":42,"state":"SHIPPED"}

    A DELETE on a GET-only path is a 405 whose Allow header survives, as RFC 9110 requires ("The origin server MUST generate an Allow header field in a 405 response"). The shipped-order cancel is a 409 with a type and two extensions a program can read.

    sequenceDiagram accTitle: A client choosing what to do with a problem response accDescr: The client sends a request and receives a problem object. It first checks the HTTP status. For a 5xx or 429 it may retry. For a 4xx it reads the type; a known type such as validation-failed highlights fields from the errors extension, an unknown type falls back to showing the title and detail. participant C as Client participant S as Server C->>S: POST /v1/orders S-->>C: 400 problem+json, type validation-failed, errors Note over C: known type: mark fields from errors C->>S: POST /v1/orders/42/cancel S-->>C: 409 problem+json, type order-already-shipped Note over C: known type: offer "contact support" C->>S: GET /v1/other S-->>C: 4xx problem+json, unknown type Note over C: unknown type: show title and detail

    Figure 3. The client's side. How to read this: the client branches on status first, then on type; an unknown type still has a title and a detail to show.

    The failure example: unexpected exceptions

    Expected output, captured from a real run (same run), scenarios 9 to 11:

    --- 9. The same error asked for as plain JSON
        request     : GET /v1/orders/7
        status      : 404
        Content-Type: application/problem+json
        body        : {"detail":"No order with id 7 exists.","instance":"/v1/orders/7","status":404,"title":"Order not found","type":"https://shopstream.example/problems/order-not-found","orderId":7}
    --- 10. An unexpected exception (the handler has a bug)
        request     : GET /v1/report
        status      : 500
        Content-Type: application/problem+json
        body        : {"detail":"The server could not complete the request.","instance":"/v1/report","status":500,"title":"Unexpected server error","type":"https://shopstream.example/problems/internal-error","requestId":"req-illustrative-1"}
        server log  : ERROR /v1/report -> IllegalStateException: could not connect to jdbc:postgresql://db-7.internal:5432/shop
    == The mistake: the exception message becomes the detail
    --- 11. The same unexpected exception
        request     : GET /v1/report
        status      : 500
        Content-Type: application/problem+json
        body        : {"detail":"could not connect to jdbc:postgresql://db-7.internal:5432/shop","instance":"/v1/report","status":500,"title":"Internal Server Error"}

    Scenario 9 asked for plain application/json and still received application/problem+json, so clients should accept that media type. In scenario 10 the catch-all returns a fixed sentence and the caller's X-Request-Id; the real message, with a database host, is only in the log line. Scenario 11 is the mistake: ex.getMessage() in detail hands that host to anyone who can trigger the error. RFC 9457 (section 5) encourages generators to avoid exposing implementation details such as a stack dump, and the Spring reference calls it good practice to customize problem details "to avoid revealing implementation details". Also, as of Spring Framework 7.1 instance is no longer set to the request path automatically (the 7.0.10 lab does), so call setInstance explicitly.

    flowchart LR accTitle: What goes to the caller and what stays on the server accDescr: An exception from a handler splits in two. The caller receives a fixed detail sentence, a type URI and a request identifier. The server log keeps the exception class, its message and the stack trace, tagged with the same request identifier. X["Exception in a handler"] --> P["To the caller:
    fixed detail, type, requestId"] X --> L["To the log:
    class, message, stack, requestId"] P -. "same id" .- L

    Figure 4. Split the cause from the message. How to read this: the request id is the join key; the caller quotes it and support finds the log line.

    Trade-offs

    Choice Gain Cost Wrong when
    Problem details everywhere One parser in every client Migrating old error shapes Clients cannot change
    A type URI per business failure Clients branch safely A catalogue to document Only generic HTTP conditions occur
    errors extension Field-level messages A member to document Consumers expect only the five members
    Fixed detail on 500 No leaks Less to read in the browser All callers are trusted internal tools

    In practice: ShopStream checkout errors

    This is the realistic example, an educational scenario on ShopStream; nothing in it describes a real company.

    This is a realistic scenario, not a case history.

    The situation

    The checkout client calls the orders API. A validation error is {"error":"bad"}, a missing item is a bare 404 and a crash is an HTML page, so the client shows one generic toast.

    Starting architecture

    Each controller writes its own error JSON; three services use three shapes.

    Options considered

    Option Behaviour Problem
    A. Keep shapes, document them No code change Clients keep three parsers
    B. A shared error JSON of our own One shape A format every new client must learn
    C. RFC 9457 through one advice Standard shape, existing libraries Every service must adopt it

    The decision

    Option C: one advice per service extending ResponseEntityExceptionHandler, a documented list of type URIs, an errors extension and a catch-all with a fixed detail and a request id.

    Rollout and verification

    Test each kind with MockMvc as in the lab, including that the 500 body never contains the exception message, then update the client to branch on type and fall back to title and detail.

    The 2 a.m. failure

    After a release a support agent pastes a customer's error that shows an internal address in detail: a new handler copied a driver exception's message into the response. Revert the handler; fix lastingly with the lab's catch-all test in every service.

    Architect's notes

    • Treat type as part of the contract. Renaming one breaks clients like renaming a field.
    • Keep one place that writes errors, and pair each response with a log line through a request id.

    Common mistakes

    • Parsing detail instead of adding an extension.
    • A body status that disagrees with the response status.
    • Exception messages in detail.

    Key takeaways

    • A problem details object has five optional members and a media type, application/problem+json; unknown extensions must be ignored.
    • type is the key clients branch on; detail is for people.
    • In Spring, extend ResponseEntityExceptionHandler and return ProblemDetail from handlers.
    • The real cause belongs in the log; the response carries a fixed sentence and an id.

    Practice exercises

    Exercise 1 (20 min). Run the lab and list, for each scenario, the status, the media type and the type.

    Exercise 2 (45 min). Add a 402-style business failure, "payment declined", with its own type and a retryable extension, and a test that the body status equals the response status.

    The other side of this topic

    The front end decides what a person sees for each kind of problem: a field message, a retry button, a support code. That half is in the front-end lesson Error, Loading and Empty States, which pairs with this one.

    Official documentation

    Ask about this article

    Get answers grounded in this post. AI-generated — based on this article, and may be imperfect.

    Scaled AI Weekly

    Enjoyed this? Get more like it every Monday.

    Real architecture decisions, LLMOps patterns that survive production, and engineering leadership advice — from 12+ years of building at enterprise scale. Free. No spam. Unsubscribe anytime.

    Get new articles by email

    Free. We email a confirmation link first, and you can unsubscribe from any email.

    Join engineers building production AI systems

    Comments