Quick revision
The short answer
Inside the handler we create the object with ProblemDetail.forStatusAndDetail(HttpStatus.CONFLICT, message), set a type URI with setType(URI.create("https://errors.example.com/duplicate-order")) and a title with setTitle, add the custom field with setProperty("orderId", id), and return the ProblemDetail directly. Spring uses its status for the response, writes it as application/problem+json, and puts orderId at the top level of the JSON.
What to remember
In this answer, we returned a 409 ProblemDetail from an @ExceptionHandler. We created it with forStatusAndDetail(HttpStatus.CONFLICT, message), set the type with setType(URI.create(...)) and the title with setTitle, and added orderId with setProperty. Returned directly, it becomes a 409 response with application/problem+json, an instance filled from the request path and orderId as a top-level member; a ResponseEntity wrapper adds headers when we need them.
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.
Inside the handler we create the object with ProblemDetail.forStatusAndDetail(HttpStatus.CONFLICT, message), set a type URI with setType(URI.create("https://errors.example.com/duplicate-order")) and a title with setTitle, add the custom field with setProperty("orderId", id), and return the ProblemDetail directly. Spring uses its status for the response, writes it as application/problem+json, and puts orderId at the top level of the JSON.
Why this question is asked
A 409 Conflict tells the client that its request was valid but clashes with the current state, for example an order with the same reference already exists. A good error body lets the client do something useful: show the existing order, or stop retrying. ProblemDetail gives us a standard place for each piece of that information. Interviewers ask this short coding question to check that we know the API and what Spring does with the returned object. In this answer, we will set up the exception, build the ProblemDetail step by step, look at the JSON that comes out, and then see how to add headers and where the handler should live.
1The Situation: a Duplicate Order
The service refuses to create a second order with the same client reference, and it throws an exception that carries the id of the order that already exists.
Without a handler, this exception would reach Spring Boot's error page as a 500. We want a 409 with a body that names the existing order.
2Building the ProblemDetail Step by Step
Here is the handler. Each line sets one member of the RFC 9457 problem details format.
forStatusAndDetail is a static factory that sets the status and the detail together; there is also forStatus when we have no detail text. Notice that type is a URI object, so we wrap the string with URI.create. The type should point to a page in our developer documentation that explains duplicate orders, and it stays stable so clients can match on it. The title stays the same for every occurrence of this type, while detail is specific to this request. setProperty adds any extra field we need; here it gives the client the id of the order that already exists.
3What the Client Receives
We return the ProblemDetail directly, without wrapping it in a ResponseEntity. Spring recognises the type, takes the HTTP status from its status field and writes it with the problem JSON media type.
| JSON member | Set by | Value |
|---|---|---|
| status | forStatusAndDetail | 409 |
| detail | forStatusAndDetail | An order with reference A-17 already exists. |
| type | setType | https://errors.example.com/duplicate-order |
| title | setTitle | Duplicate order |
| instance | Spring, from the request path | /orders |
orderId | setProperty | 812 |
Two things happen that we did not write. First, instance is filled with the request path, because Spring sets it when we leave it empty; check your version for this behaviour. Second, orderId appears next to the standard fields rather than inside a nested properties object. Spring registers a Jackson mixin for ProblemDetail that writes the property map as top-level members, which is what the RFC expects for extensions.
4Adding Headers With ResponseEntity
Sometimes the client also needs a header, for example a Location pointing at the existing order. Then we wrap the ProblemDetail in a ResponseEntity and still get the same body and content type.
The status in the ResponseEntity and in the ProblemDetail should agree; it is simplest to use the same constant for both.
5Where the Handler Lives
Placed inside OrderController, the handler only covers that controller. Since a duplicate order can be raised by several endpoints, it usually belongs in a @RestControllerAdvice. An alternative is to make DuplicateOrderException extend ErrorResponseException and build the ProblemDetail in its constructor, so it carries its own response and needs no handler at all, as long as an advice extending ResponseEntityExceptionHandler, or Spring Boot's problem details support, is active to render it.
6Follow-up questions
Why use a type URI instead of a short code?
The RFC's identifier for the kind of problem is the type URI, and a URI can point to documentation. Many teams add a short code extension such as duplicate_order as well, because it is easier to match in client code. Both must stay stable once published.
Is 409 the right status for a duplicate?
Yes, 409 Conflict fits a request that conflicts with the current state of the resource. Some APIs instead make creation idempotent: a repeated request with the same idempotency key returns the original 201 response, which is friendlier for clients that retry after a timeout.
Is it safe to put ex.getMessage() into detail?
Here yes, because we wrote this exception's message for clients. For exceptions from libraries or the database we never copy the message, since it can reveal internal details; we use a fixed text and log the original.
7Conclusion
In this answer, we returned a 409 ProblemDetail from an @ExceptionHandler. We created it with forStatusAndDetail(HttpStatus.CONFLICT, message), set the type with setType(URI.create(...)) and the title with setTitle, and added orderId with setProperty. Returned directly, it becomes a 409 response with application/problem+json, an instance filled from the request path and orderId as a top-level member; a ResponseEntity wrapper adds headers when we need them.
Open with
In the handler for DuplicateOrderException I create the body with ProblemDetail.forStatusAndDetail, passing HttpStatus.CONFLICT and the exception's message, which I wrote myself so it's safe to show. That sets status 409 and the detail.
Then, if they want more
Then I call setType with URI.create of a documentation URL like errors.example.com slash duplicate-order, because the type identifies the kind of problem and should be stable. I call setTitle with Duplicate order, which is the same for every occurrence, while detail is specific to this request.
Then setProperty of orderId with the id of the existing order, so the client can show it. I return the ProblemDetail directly. Spring takes the status from it, writes it as application/problem+json, fills instance with the request path when I leave it empty, and serializes orderId as a top-level member next to the standard fields, thanks to a Jackson mixin.
If I also want a Location header to the existing order, I wrap it in ResponseEntity.status CONFLICT with location and body. I'd put the handler in a @RestControllerAdvice because duplicates can come from several endpoints, or make the exception extend ErrorResponseException so it carries its own ProblemDetail.
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 "Return a ProblemDetail with status 409, a type URI and a custom property orderId from an...": https://scrivio-interview-library.netlify.app/spring-boot/error-handling/5cfd653c38/