Class ProblemDetailFactory

java.lang.Object
io.leandev.appfuse.error.ProblemDetailFactory

public final class ProblemDetailFactory extends Object

RFC 7807/9457 ProblemDetail 工廠類別

提供建立標準化 ProblemDetail 的工具方法。

RFC 7807 標準欄位對應

  • type - 問題類型 URI(預設 about:blank)
  • title - 問題標題(HTTP 狀態描述)
  • status - HTTP 狀態碼
  • detail - 問題詳細描述
  • instance - 發生問題的請求路徑

自訂擴充欄位

  • violations - 驗證錯誤詳情
  • errorCode - 應用程式錯誤代碼
  • format - 訊息模板(用於國際化)
  • params - 訊息參數
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.springframework.http.ProblemDetail
    建立帳號停用錯誤
    static org.springframework.http.ProblemDetail
    建立帳號過期錯誤
    static org.springframework.http.ProblemDetail
    建立帳號鎖定錯誤
    static org.springframework.http.ProblemDetail
    建立無效憑證錯誤
    static org.springframework.http.ProblemDetail
    建立 400 Bad Request
    static org.springframework.http.ProblemDetail
    conflict(String detail)
    建立 409 Conflict
    static org.springframework.http.ProblemDetail
    create(org.springframework.http.HttpStatus status, String detail)
    建立基本的 ProblemDetail
    static org.springframework.http.ProblemDetail
    create(org.springframework.http.HttpStatus status, String type, String detail)
    建立帶有自訂類型的 ProblemDetail
    static org.springframework.http.ProblemDetail
    建立憑證過期錯誤
    static org.springframework.http.ProblemDetail
    forbidden(String detail)
    建立 403 Forbidden
    static org.springframework.http.ProblemDetail
    fromStatusCode(int statusCode, String detail)
    從 HTTP 狀態碼建立 ProblemDetail
    static org.springframework.http.ProblemDetail
    建立 500 Internal Server Error
    static org.springframework.http.ProblemDetail
    notFound(String detail)
    建立 404 Not Found
    static org.springframework.http.ProblemDetail
    建立 401 Unauthorized
    static org.springframework.http.ProblemDetail
    建立 422 Unprocessable Entity(業務規則違反)
    static org.springframework.http.ProblemDetail
    validationError(String detail, Set<Violation> violations)
    建立驗證錯誤的 ProblemDetail
    static org.springframework.http.ProblemDetail
    withErrorCode(org.springframework.http.ProblemDetail problem, String errorCode)
    設定錯誤代碼
    static org.springframework.http.ProblemDetail
    withI18n(org.springframework.http.ProblemDetail problem, String format, Object[] params)
    設定國際化參數

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • create

      public static org.springframework.http.ProblemDetail create(org.springframework.http.HttpStatus status, String detail)
      建立基本的 ProblemDetail
      Parameters:
      status - HTTP 狀態
      detail - 詳細描述
      Returns:
      ProblemDetail
    • create

      public static org.springframework.http.ProblemDetail create(org.springframework.http.HttpStatus status, String type, String detail)
      建立帶有自訂類型的 ProblemDetail
      Parameters:
      status - HTTP 狀態
      type - 問題類型(會加上基礎 URI 前綴)
      detail - 詳細描述
      Returns:
      ProblemDetail
    • badRequest

      public static org.springframework.http.ProblemDetail badRequest(String detail)
      建立 400 Bad Request
    • unauthorized

      public static org.springframework.http.ProblemDetail unauthorized(String detail)
      建立 401 Unauthorized
    • accountLocked

      public static org.springframework.http.ProblemDetail accountLocked(String detail)
      建立帳號鎖定錯誤
    • accountDisabled

      public static org.springframework.http.ProblemDetail accountDisabled(String detail)
      建立帳號停用錯誤
    • accountExpired

      public static org.springframework.http.ProblemDetail accountExpired(String detail)
      建立帳號過期錯誤
    • credentialsExpired

      public static org.springframework.http.ProblemDetail credentialsExpired(String detail)
      建立憑證過期錯誤
    • badCredentials

      public static org.springframework.http.ProblemDetail badCredentials(String detail)
      建立無效憑證錯誤
    • forbidden

      public static org.springframework.http.ProblemDetail forbidden(String detail)
      建立 403 Forbidden
    • notFound

      public static org.springframework.http.ProblemDetail notFound(String detail)
      建立 404 Not Found
    • conflict

      public static org.springframework.http.ProblemDetail conflict(String detail)
      建立 409 Conflict
    • unprocessableEntity

      public static org.springframework.http.ProblemDetail unprocessableEntity(String detail)
      建立 422 Unprocessable Entity(業務規則違反)
    • internalError

      public static org.springframework.http.ProblemDetail internalError(String detail)
      建立 500 Internal Server Error
    • validationError

      public static org.springframework.http.ProblemDetail validationError(String detail, Set<Violation> violations)
      建立驗證錯誤的 ProblemDetail
      Parameters:
      detail - 詳細描述
      violations - 驗證違規集合
      Returns:
      ProblemDetail
    • withErrorCode

      public static org.springframework.http.ProblemDetail withErrorCode(org.springframework.http.ProblemDetail problem, String errorCode)
      設定錯誤代碼
    • withI18n

      public static org.springframework.http.ProblemDetail withI18n(org.springframework.http.ProblemDetail problem, String format, Object[] params)
      設定國際化參數
    • fromStatusCode

      public static org.springframework.http.ProblemDetail fromStatusCode(int statusCode, String detail)
      從 HTTP 狀態碼建立 ProblemDetail