RFC 7807 Problem Details and Consistent Error Responses
Return structured error payloads conforming to RFC 7807 using Spring 6's ProblemDetail.
RFC 7807 Problem Details and Consistent Error Responses is a free Java Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is RFC 7807?
RFC 7807 "Problem Details for HTTP APIs" defines a standard JSON format for error responses. It avoids custom error formats per API and gives clients a predictable structure to parse.
RFC 7807 Fields
Standard fields: type (URI identifying the problem), title (human-readable summary), status (HTTP status code), detail (specific explanation), instance (URI of the specific occurrence).
{
"type": "https://api.example.com/errors/not-found",
"title": "Resource Not Found",
"status": 404,
"detail": "User with id 42 does not exist.",
"instance": "/api/users/42"
}Spring 6 ProblemDetail
Spring 6 / Spring Boot 3 ships with built-in ProblemDetail support. Return ProblemDetail from exception handlers or use ErrorResponseException.
import org.springframework.http.ProblemDetail;
@ExceptionHandler(ResourceNotFoundException.class)
public ProblemDetail handleNotFound(ResourceNotFoundException ex, HttpServletRequest req) {
ProblemDetail pd = ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, ex.getMessage());
pd.setType(URI.create("https://api.example.com/errors/not-found"));
pd.setTitle("Resource Not Found");
pd.setInstance(URI.create(req.getRequestURI()));
return pd;
}Adding Custom Extensions
ProblemDetail supports extension properties via setProperty(key, value) for domain-specific details like error codes or field errors.
ProblemDetail pd = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST);
pd.setTitle("Validation Failed");
pd.setProperty("errors", fieldErrors); // custom extension
pd.setProperty("timestamp", Instant.now());Enabling RFC 7807 for Spring MVC
Enable ProblemDetail for all built-in Spring exceptions by setting spring.mvc.problemdetails.enabled=true in application.properties. Spring then wraps standard exceptions (404, 405, etc.) in RFC 7807 format automatically.
# application.properties:
spring.mvc.problemdetails.enabled=trueErrorResponseException
Throw ErrorResponseException from service code to produce an RFC 7807 response without a handler method — Spring MVC catches and formats it.
throw new ErrorResponseException(HttpStatus.CONFLICT,
ProblemDetail.forStatusAndDetail(HttpStatus.CONFLICT,
"Email already exists: " + email), null);Custom ProblemDetail Subclass
Create a domain-specific subclass of ProblemDetail to add typed extension fields and keep handler code clean.
public class ValidationProblemDetail extends ProblemDetail {
private final Map<String, String> fieldErrors;
public ValidationProblemDetail(Map<String, String> errors) {
super(HttpStatus.BAD_REQUEST.value());
this.fieldErrors = errors;
setTitle("Validation Failed");
setProperty("fieldErrors", errors);
}
}Content Type: application/problem+json
RFC 7807 responses should use the content type application/problem+json so clients can distinguish problem responses from normal JSON payloads.
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.contentType(MediaType.APPLICATION_PROBLEM_JSON)
.body(pd);Consistent Error Structure Checklist
A good error API: (1) machine-readable type URI, (2) human-readable title, (3) exact HTTP status code, (4) specific detail message, (5) request instance URI, (6) optional extension fields (timestamp, traceId, field errors).
Trace IDs for Observability
Add the request's trace ID (from Micrometer Tracing or MDC) as an extension property so engineers can correlate error logs with the specific failing request.
pd.setProperty("traceId", MDC.get("traceId"));
pd.setProperty("timestamp", Instant.now());Testing Problem Details
In @WebMvcTest tests, assert the response content type is application/problem+json and JSON fields like status, title, and detail match expected values.
mockMvc.perform(get("/api/users/999"))
.andExpect(status().isNotFound())
.andExpect(content().contentType("application/problem+json"))
.andExpect(jsonPath("$.status").value(404))
.andExpect(jsonPath("$.title").value("Resource Not Found"));Quick Check
Which Spring Boot property enables RFC 7807 for built-in Spring MVC exceptions?
Recap
RFC 7807 standardizes JSON error responses with type, title, status, detail, and instance fields. Spring 6 provides ProblemDetail and ErrorResponseException. Enable with spring.mvc.problemdetails.enabled=true. Add traceId and timestamp as extensions for observability.
Frequently asked questions
Is the “RFC 7807 Problem Details and Consistent Error Responses” lesson free?
Yes — the full text of “RFC 7807 Problem Details and Consistent Error Responses” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “RFC 7807 Problem Details and Consistent Error Responses”?
Return structured error payloads conforming to RFC 7807 using Spring 6's ProblemDetail. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “RFC 7807 Problem Details and Consistent Error Responses” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Bean Validation: @NotNull, @Size, @Pattern
- Custom Constraint Annotations
- Global Exception Handling with @ControllerAdvice
- RFC 7807 Problem Details and Consistent Error Responses