Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is ProblemDetail in Spring Framework 6 and how does it relate to error responses in a REST API?

ProblemDetail is Spring Framework 6's class for the problem details format of RFC 9457, which replaced RFC 7807: an error body with type, title, status, detail and instance, plus any extra properties we add. Controllers and @ExceptionHandler methods can return a ProblemDetail or an ErrorResponse, ErrorResponseException carries one, and ResponseEntityExceptionHandler renders Spring MVC's own exceptions in this format. Spring Boot can turn that on for built-in exceptions with spring.mvc.problemdetails.enabled (spring.webflux.problemdetails.enabled for WebFlux), and responses use the application/problem+json content type.

Why this question is asked

Every API returns errors, and clients have to parse them. Without a standard, each service invents its own shape, error in one, message in another, code in a third, and every client needs special handling. Problem details is the IETF's standard shape for HTTP API errors, and since Spring Framework 6 it is built in. Interviewers ask about it to see whether we know the format and how Spring wires it, and whether we care about consistency more than about any particular format. In this answer, we will recap the format, see ProblemDetail and ErrorResponse in code, look at how Spring handles its own exceptions, enable it in Spring Boot, and add a machine-readable error code.

1A Quick Recap of the Problem Details Format

RFC 9457, published in 2023 and replacing RFC 7807, defines a JSON object for describing an HTTP API error. It has five standard members. type is a URI that identifies the kind of problem, ideally pointing to documentation; when absent it means about:blank, which says the problem is simply the HTTP status. title is a short human-readable summary of that kind of problem. status repeats the HTTP status code. detail is a human-readable explanation of this particular occurrence. instance is a URI identifying this occurrence, often the request path. Beyond these, a problem can carry extension members, any extra fields that the API defines.

The response uses the media type application/problem+json, so clients can recognise an error body by its content type alone.

YAML
HTTP/1.1 404 Not Found
Content-Type: application/problem+json
{
"type": "https://api.example.com/problems/order-not-found",
"title": "Order not found",
"status": 404,
"detail": "Order 5 does not exist",
"instance": "/orders/5",
"errorCode": "ORDER_NOT_FOUND"
}
Members of a problem detail
MemberMeaningExample
typeURI for the kind of problemhttps://api.example.com/problems/order-not-found
titleShort summary of that kindOrder not found
statusHTTP status code404
detailExplanation of this occurrenceOrder 5 does not exist
instanceURI of this occurrence/orders/5
extensionsAny extra members the API defineserrorCode, errors, traceId

2Returning ProblemDetail From Spring MVC

ProblemDetail is a plain class in spring-web with fields for the standard members and a map of extra properties. We can return it from an @ExceptionHandler, or wrap it in a ResponseEntity, and Spring writes it with the application/problem+json content type. Its status is used as the HTTP status of the response.

Java
@RestControllerAdvice
class ApiErrors {
@ExceptionHandler(OrderNotFoundException.class)
ProblemDetail orderNotFound(OrderNotFoundException ex, HttpServletRequest req) {
ProblemDetail pd = ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND,
"Order " + ex.orderId() + " does not exist");
pd.setType(URI.create("https://api.example.com/problems/order-not-found"));
pd.setTitle("Order not found");
pd.setInstance(URI.create(req.getRequestURI()));
pd.setProperty("errorCode", "ORDER_NOT_FOUND");
return pd; // 404, Content-Type: application/problem+json
}
}

If we do not set type, Spring leaves it as about:blank, and if we do not set title, Spring fills in the reason phrase of the status, such as Not Found. Notice that the extra property is written at the top level of the JSON, next to the standard members, as the RFC expects; Spring's Jackson support takes care of that.

3ErrorResponse and ErrorResponseException

ErrorResponse is an interface for anything that can describe itself as an HTTP error: it exposes a status code, headers and a ProblemDetail body. ErrorResponseException is a ready-made RuntimeException that implements it. We can throw it, or subclass it for our own exceptions, and Spring MVC turns it into a problem details response without any handler method.

Java
public class OrderNotFoundException extends ErrorResponseException {
public OrderNotFoundException(long id) {
super(HttpStatus.NOT_FOUND, asProblem(id), null);
}
private static ProblemDetail asProblem(long id) {
ProblemDetail pd = ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, "Order " + id + " does not exist");
pd.setType(URI.create("https://api.example.com/problems/order-not-found"));
pd.setProperty("errorCode", "ORDER_NOT_FOUND");
return pd;
}
}
// In a controller or service
throw new OrderNotFoundException(5); // 404 problem detail, no @ExceptionHandler needed

This keeps the error's HTTP description next to the error itself. Some teams prefer to keep domain exceptions free of HTTP concerns and map them in one @RestControllerAdvice instead; both are reasonable as long as the team picks one.

4Spring's Own Exceptions and ResponseEntityExceptionHandler

Spring MVC raises its own exceptions for framework-level errors: MethodArgumentNotValidException for failed @Valid validation, HttpMessageNotReadableException for malformed JSON, HttpRequestMethodNotSupportedException for a wrong method, NoResourceFoundException for unknown paths in recent versions, and others. Since Spring Framework 6 these implement ErrorResponse, so each carries a ProblemDetail.

ResponseEntityExceptionHandler is a base class for a @ControllerAdvice that handles all of them and renders the problem details. We extend it to get consistent responses for framework errors and override individual methods to enrich them, for example adding field errors to validation failures.

Java
@RestControllerAdvice
class ApiExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
HttpHeaders headers, HttpStatusCode status, WebRequest request) {
ProblemDetail pd = ex.getBody(); // already a 400 problem detail
pd.setProperty("errorCode", "VALIDATION_FAILED");
pd.setProperty("errors", ex.getFieldErrors().stream()
.map(f -> Map.of("field", f.getField(), "message", f.getDefaultMessage())).toList());
return handleExceptionInternal(ex, pd, headers, status, request);
}
}
Step throughHow an exception becomes a problem detail
  1. Exception thrownOurs or Spring MVC's
  2. Matching @ExceptionHandler?In a @ControllerAdvice
    YesHandler returns ProblemDetailNoCheck ErrorResponse
  3. Implements ErrorResponse?Spring MVC exceptions do
    YesUse its ProblemDetailNoDefault error handling
  4. Write application/problem+jsonStatus from the ProblemDetail

4 steps in order. Play them, or step through them one at a time.

ResponseEntityExceptionHandler, or the Spring Boot property, covers Spring MVC's own exceptions.

5Enabling It in Spring Boot

Spring Boot does not switch the format on for everything by default. Setting spring.mvc.problemdetails.enabled=true makes Boot register a ResponseEntityExceptionHandler-based advice, so Spring MVC's built-in exceptions are rendered as problem details without any code. For WebFlux the property is spring.webflux.problemdetails.enabled. If we write our own subclass of ResponseEntityExceptionHandler, as above, we do not need the property, because our advice already does that job.

Properties
# application.properties
spring.mvc.problemdetails.enabled=true
# curl -X POST /orders -H 'Content-Type: application/json' -d '{bad json'
# HTTP/1.1 400 Content-Type: application/problem+json
# {"type":"about:blank","title":"Bad Request","status":400,
# "detail":"Failed to read request","instance":"/orders"}

Notice that the default detail texts are deliberately generic. They avoid leaking internals such as parser messages, which is what we want on a public API. Finally, a word on priorities: the exact error format matters less to clients than having one consistent shape across every endpoint and service. Problem details is a good choice because it is a standard and Spring supports it, but the real win is consistency.

6Follow-up questions

How do you add a machine-readable error code to a ProblemDetail?

With setProperty, for example pd.setProperty("errorCode", "ORDER_NOT_FOUND"), which Spring writes as a top-level member next to type and title. The type URI already identifies the kind of problem, so some teams use it as the code, but a short stable string is easier for clients to switch on. I would define the codes in one place, document them, and never change an existing code's meaning.

Should the detail text be shown to end users?

Usually not directly. detail is meant for developers and may be in English only. Clients should switch on the type or error code and show their own localised message. That is another reason for a stable machine-readable code.

Can we use problem details with Spring Boot's /error page handling?

Errors that never reach Spring MVC's exception handling, such as some servlet container errors, still go through Boot's error controller. With problem details enabled, most MVC errors are covered, but it is worth testing a few edge cases like unknown paths and oversized requests to see what shape they produce in your version.

7Conclusion

In this answer, we saw that ProblemDetail models the RFC 9457 problem details format with type, title, status, detail, instance and extra properties. Handlers can return ProblemDetail or ErrorResponse, ErrorResponseException carries one, ResponseEntityExceptionHandler renders Spring MVC's own exceptions this way, Spring Boot enables it for built-in exceptions with spring.mvc.problemdetails.enabled or spring.webflux.problemdetails.enabled, and responses use application/problem+json. A consistent error shape is what clients value most.

What to say in the interviewAbout 80 seconds aloud

Open with

ProblemDetail is Spring Framework 6's representation of the problem details format from RFC 9457, which replaced RFC 7807. It is a standard JSON error body with type, a URI for the kind of problem that defaults to about:blank, title, status, detail for this occurrence, and instance, plus any extension properties I add with setProperty, which are written at the top level.

Then, if they want more

In Spring MVC I can return a ProblemDetail from an ExceptionHandler, and Spring writes it with the application/problem+json content type and uses its status. There is also the ErrorResponse interface, and ErrorResponseException implements it, so I can throw or subclass it and get a problem response without a handler.

Spring's own exceptions, like MethodArgumentNotValidException or HttpMessageNotReadableException, implement ErrorResponse too, and ResponseEntityExceptionHandler is a base advice that renders all of them as problem details; I extend it to enrich them, for example adding field errors and an errorCode to validation failures. In Spring Boot, spring.mvc.problemdetails.enabled, or the webflux equivalent, turns that on without code.

For a machine-readable code I add an errorCode property with a stable documented value. The main point for clients, though, is one consistent error shape everywhere.

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.
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 "What is ProblemDetail in Spring Framework 6 and how does it relate to error responses in...": https://scrivio-interview-library.netlify.app/spring-boot/rest-api-design/c56108f91b/