Record Class ProblemDetail

java.lang.Object
java.lang.Record
cloud.opencode.base.web.problem.ProblemDetail
Record Components:
type - the problem type URI, defaults to "about:blank" | 问题类型 URI,默认 "about:blank"
title - the human-readable summary | 人类可读的摘要
status - the HTTP status code | HTTP 状态码
detail - the human-readable explanation | 人类可读的详细说明
instance - the problem occurrence URI | 问题实例 URI
extensions - the extension members | 扩展属性

public record ProblemDetail(String type, String title, int status, String detail, String instance, Map<String,Object> extensions) extends Record
RFC 9457 Problem Details for HTTP APIs RFC 9457 HTTP API 问题详情

A machine-readable format for conveying error details in HTTP response bodies, as defined by RFC 9457.

一种机器可读的格式,用于在 HTTP 响应体中传递错误详情, 由 RFC 9457 定义。

Features | 主要功能:

  • RFC 9457 compliant problem details - 符合 RFC 9457 的问题详情
  • Builder pattern for flexible construction - 构建器模式灵活构造
  • Factory methods for common use cases - 常见场景的工厂方法
  • Extension properties support - 扩展属性支持
  • OpenWebException integration - OpenWebException 集成

Usage Examples | 使用示例:

// Quick creation
ProblemDetail problem = ProblemDetail.of(404, "User not found");

// With builder
ProblemDetail problem = ProblemDetail.builder()
    .type("https://example.com/not-found")
    .title("Not Found")
    .status(404)
    .detail("User with ID 42 was not found")
    .instance("/users/42")
    .extension("userId", 42)
    .build();

// From exception
ProblemDetail problem = ProblemDetail.fromException(openWebException);

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是(不可变记录)
  • Null-safe: Yes (null fields treated as absent) - 空值安全: 是(null 字段视为缺失)
Since:
JDK 25, opencode-base-web V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

    • CONTENT_TYPE

      public static final String CONTENT_TYPE
      Content-Type for problem details responses. 问题详情响应的 Content-Type。
      See Also:
    • DEFAULT_TYPE

      public static final String DEFAULT_TYPE
      Default problem type URI as defined in RFC 9457. RFC 9457 定义的默认问题类型 URI。
      See Also:
  • Constructor Details

    • ProblemDetail

      public ProblemDetail(String type, String title, int status, String detail, String instance, Map<String,Object> extensions)
      Compact constructor: defensively copies extensions to ensure immutability. 紧凑构造器:防御性复制扩展属性以确保不可变性。
  • Method Details

    • of

      public static ProblemDetail of(int status, String title, String detail)
      Create a ProblemDetail with status, title, and detail. 使用状态码、标题和详细说明创建 ProblemDetail。
      Parameters:
      status - the HTTP status code | HTTP 状态码
      title - the title | 标题
      detail - the detail | 详细说明
      Returns:
      the problem detail | 问题详情
    • of

      public static ProblemDetail of(int status, String detail)
      Create a ProblemDetail with status and detail; title is auto-derived from status. 使用状态码和详细说明创建 ProblemDetail;标题从状态码自动推导。
      Parameters:
      status - the HTTP status code | HTTP 状态码
      detail - the detail | 详细说明
      Returns:
      the problem detail | 问题详情
    • fromException

      public static ProblemDetail fromException(OpenWebException e)
      Create a ProblemDetail from an OpenWebException. 从 OpenWebException 创建 ProblemDetail。
      Parameters:
      e - the exception | 异常
      Returns:
      the problem detail | 问题详情
      Throws:
      NullPointerException - if e is null | 如果 e 为 null
    • fromException

      public static ProblemDetail fromException(OpenWebException e, boolean includeCode)
      Create a ProblemDetail from an OpenWebException, optionally including the internal error code. 从 OpenWebException 创建 ProblemDetail,可选择是否包含内部错误码。
      Parameters:
      e - the exception | 异常
      includeCode - whether to include the internal error code in extensions | 是否在扩展中包含内部错误码
      Returns:
      the problem detail | 问题详情
      Throws:
      NullPointerException - if e is null | 如果 e 为 null
    • builder

      public static ProblemDetail.Builder builder()
      Create a new Builder. 创建新的构建器。
      Returns:
      the builder | 构建器
    • hasExtensions

      public boolean hasExtensions()
      Check whether this problem has extension properties. 检查是否包含扩展属性。
      Returns:
      true if extensions are present | 如果存在扩展属性返回 true
    • getContentType

      public String getContentType()
      Get the content type for problem detail responses. 获取问题详情响应的 Content-Type。
      Returns:
      "application/problem+json"
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • type

      public String type()
      Returns the value of the type record component.
      Returns:
      the value of the type record component
    • title

      public String title()
      Returns the value of the title record component.
      Returns:
      the value of the title record component
    • status

      public int status()
      Returns the value of the status record component.
      Returns:
      the value of the status record component
    • detail

      public String detail()
      Returns the value of the detail record component.
      Returns:
      the value of the detail record component
    • instance

      public String instance()
      Returns the value of the instance record component.
      Returns:
      the value of the instance record component
    • extensions

      public Map<String,Object> extensions()
      Returns the value of the extensions record component.
      Returns:
      the value of the extensions record component