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.
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.
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.
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.
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
typeas 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
detailinstead 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. typeis the key clients branch on;detailis for people.- In Spring, extend
ResponseEntityExceptionHandlerand returnProblemDetailfrom 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.
Related reading
- API Design: the contract these errors belong to.
- Input Validation and Injection Prevention: what produces the 400s.
- Frontend: Error, Loading and Empty States: showing these errors to users.
Official documentation
- IETF, RFC 9457: Problem Details for HTTP APIs: the members, extensions, security considerations and registry.
- IETF, RFC 9110: HTTP Semantics: the meaning of the status codes, including the
Allowheader on a 405. - Spring Framework, Error Responses:
ProblemDetail,ErrorResponse,ResponseEntityExceptionHandler, customization and i18n.