Spring Boot 4 Microservices & REST APIs · บทเรียน

รายละเอียดปัญหา (RFC 7807)

ส่งการตอบกลับข้อผิดพลาดตามมาตรฐาน

บทเรียน 4 จาก 413 ขั้นตอน

รายละเอียดปัญหา (RFC 7807) เป็นบทเรียน Spring Boot 4 Microservices & REST APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 kind
  • title — short, human-readable summary
  • status — the HTTP status code
  • detail — specifics for this occurrence
  • instance — 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: true

Extending 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 type for 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 ProblemDetail models it; served as application/problem+json
  • Enable built-in conversion via spring.mvc.problemdetails.enabled
  • Add extension members with setProperty
  • Extend ResponseEntityExceptionHandler to shape framework errors
เริ่มต้นได้ฟรี

เรียนรู้ Java ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
24
บทเรียน
93

คำถามที่พบบ่อย

บทเรียน “รายละเอียดปัญหา (RFC 7807)” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “รายละเอียดปัญหา (RFC 7807)” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Microservices & REST APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Microservices & REST APIs มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “รายละเอียดปัญหา (RFC 7807)”

ส่งการตอบกลับข้อผิดพลาดตามมาตรฐาน คุณปฏิบัติ Spring Boot 4 Microservices & REST APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Microservices & REST APIs หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Microservices & REST APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “รายละเอียดปัญหา (RFC 7807)” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Microservices & REST APIs นี้ได้ไหม

ได้ บทเรียน Spring Boot 4 Microservices & REST APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การตรวจสอบบีนด้วย @Valid
  2. ตัวตรวจสอบแบบกำหนดเอง
  3. @ControllerAdvice และ @ExceptionHandler
  4. รายละเอียดปัญหา (RFC 7807)
← กลับไปที่ Spring Boot 4 Microservices & REST APIs