问题详情(RFC 7807)
返回标准化的错误响应
问题详情(RFC 7807) 是 CoddyKit 上的免费 Spring Boot 4 Microservices & REST APIs 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Spring Boot 4 Microservices & REST APIs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Spring Boot 4 Microservices & REST APIs 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
A Standard for API Errors
Every team inventing its own error JSON leads to chaos for API consumers. RFC 7807 defines Problem Details — a standard, machine-readable error format with a media type of application/problem+json.
The Problem Details Fields
The standard defines a small set of fields:
type— a URI identifying the problem kindtitle— short, human-readable summarystatus— the HTTP status codedetail— specifics for this occurrenceinstance— URI of the specific occurrence
Spring’s ProblemDetail
Spring Framework 6 / Boot 3 ship a built-in ProblemDetail class that models RFC 7807 directly. You construct it with a status and customize the standard fields.
ProblemDetail pd = ProblemDetail.forStatusAndDetail(
HttpStatus.NOT_FOUND, "Order 42 was not found");
pd.setTitle("Order Not Found");
pd.setType(URI.create("https://errors.example.com/order-not-found"));Returning ProblemDetail
Return a ProblemDetail from a controller or exception handler. Spring serializes it as application/problem+json with the right status.
@ExceptionHandler(OrderNotFoundException.class)
public ProblemDetail handle(OrderNotFoundException ex) {
ProblemDetail pd = ProblemDetail.forStatusAndDetail(
HttpStatus.NOT_FOUND, ex.getMessage());
pd.setTitle("Order Not Found");
return pd;
}Enabling Built-in Problem Details
Spring Boot can convert its own framework exceptions into Problem Details automatically when you enable the property, giving consistent errors even for built-in failures.
spring:
mvc:
problemdetails:
enabled: trueExtending with Custom Properties
RFC 7807 permits extension members. Add domain-specific data — an error code, a correlation id, or field errors — via setProperty.
pd.setProperty("errorCode", "ORDER_NOT_FOUND");
pd.setProperty("correlationId", correlationId);
pd.setProperty("timestamp", Instant.now());ResponseEntityExceptionHandler
Extend ResponseEntityExceptionHandler in your @ControllerAdvice to inherit handlers for Spring’s standard exceptions, then override the hooks to shape them as Problem Details.
@RestControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
// override protected handle... methods to customize
}Problem Details for Validation
Override the validation hook to express field errors as a Problem Details body with an extension array, giving clients structured, standard validation feedback.
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex, HttpHeaders h,
HttpStatusCode status, WebRequest req) {
ProblemDetail pd = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST);
pd.setTitle("Validation Failed");
pd.setProperty("errors", ex.getBindingResult().getFieldErrors().stream()
.map(e -> Map.of("field", e.getField(), "message", e.getDefaultMessage()))
.toList());
return ResponseEntity.badRequest().body(pd);
}Why the type URI Matters
The type URI gives each problem a stable identity clients can branch on — more robust than parsing human text. Point it at documentation describing the error and how to recover.
Content Negotiation
Problem Details responses carry the application/problem+json content type. Clients can detect this media type to distinguish structured errors from normal success payloads.
HTTP/1.1 404 Not Found
Content-Type: application/problem+json
{ "type": "...", "title": "Order Not Found", "status": 404, "detail": "..." }Adopting the Standard
Using Problem Details makes your API errors predictable and interoperable:
- One shape across all endpoints and frameworks
- Machine-readable
typefor client logic - Extensible for domain-specific detail
Quick Check
Test your understanding of RFC 7807.
Recap
Problem Details standardize API errors.
- RFC 7807 defines
type,title,status,detail,instance - Spring’s
ProblemDetailmodels it; served asapplication/problem+json - Enable built-in conversion via
spring.mvc.problemdetails.enabled - Add extension members with
setProperty - Extend
ResponseEntityExceptionHandlerto shape framework errors
常见问题解答
「问题详情(RFC 7807)」课时是免费的吗?
是的 — 「问题详情(RFC 7807)」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Spring Boot 4 Microservices & REST APIs 课程的其余内容,请升级到 CoddyKit PRO。 Spring Boot 4 Microservices & REST APIs 课程共包含 4 节课。
「问题详情(RFC 7807)」这节课中我会学到什么?
返回标准化的错误响应 你通过在浏览器中直接运行的动手代码来练习 Spring Boot 4 Microservices & REST APIs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Spring Boot 4 Microservices & REST APIs 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Spring Boot 4 Microservices & REST APIs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「问题详情(RFC 7807)」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Spring Boot 4 Microservices & REST APIs 课中编写并运行代码吗?
能。每节 Spring Boot 4 Microservices & REST APIs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。