Class JsonApiError

java.lang.Object
com.toedter.spring.hateoas.jsonapi.JsonApiError

public final class JsonApiError extends Object
Represents a single JSON:API error object compliant with the JSON:API specification.

This class provides an immutable representation of an error that can be included in JsonApiErrors collections. Each error object can contain:

  • A unique identifier for this specific error occurrence
  • Links to additional information (e.g., about link)
  • HTTP status code applicable to the error
  • Application-specific error code
  • Human-readable summary (title)
  • Detailed explanation of the error
  • Source information (pointer or parameter causing the error)
  • Additional meta information

The class uses Lombok's @With annotation for convenient error building:


 JsonApiError error = JsonApiError.create()
   .withStatus("404")
   .withTitle("Resource not found")
   .withDetail("The requested movie does not exist");
 
See Also:
  • Constructor Details

    • JsonApiError

      public JsonApiError()
      Creates an empty JsonApiError.
  • Method Details

    • withSourcePointer

      public JsonApiError withSourcePointer(String sourcePointer)
      Adds a source pointer to the error.
      Parameters:
      sourcePointer - a JSON Pointer [RFC6901] to the associated entity in the request document [e.g. "/data" for a primary data object, or "/data/attributes/title" for a specific attribute].
      Returns:
      will never be null.
    • withSourceParameter

      public JsonApiError withSourceParameter(String sourceParameter)
      Adds a source parameter to the error.
      Parameters:
      sourceParameter - a string indicating which URI query parameter caused the error.
      Returns:
      will never be null.
    • withAboutLink

      public JsonApiError withAboutLink(String aboutLink)
      Adds an about link to the error.
      Parameters:
      aboutLink - the link describing this error
      Returns:
      will never be null.
    • create

      public static JsonApiError create()
      Creates an empty JsonApiError.
      Returns:
      an empty JsonApiError.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getId

      public String getId()
      Gets a unique identifier for this particular occurrence of the problem.
      Returns:
      will never be null. -- WITHER -- Sets a unique identifier for this particular occurrence of the problem.
    • withId

      public JsonApiError withId(String id)
      Gets a unique identifier for this particular occurrence of the problem.
      Parameters:
      id - the unique identifier of the error.
      Returns:
      a clone of this object, except with this updated property (returns this if an identical value is passed).
    • getLinks

      public Map<String,String> getLinks()
      Gets a links object containing the following members: about: a link that leads to further details about this particular occurrence of the problem.
      Returns:
      will never be null.
    • getStatus

      public String getStatus()
      Gets the HTTP status code applicable to this problem, expressed as a string value.
      Returns:
      will never be null. -- WITHER -- Adds the HTTP status code applicable to this problem, expressed as a string value.
    • withStatus

      public JsonApiError withStatus(String status)
      Gets the HTTP status code applicable to this problem, expressed as a string value.
      Parameters:
      status - the HTTP status code.
      Returns:
      a clone of this object, except with this updated property (returns this if an identical value is passed).
    • getCode

      public String getCode()
      Gets an application-specific error code, expressed as a string value.
      Returns:
      will never be null. -- WITHER -- Sets an application-specific error code, expressed as a string value.
    • withCode

      public JsonApiError withCode(String code)
      Gets an application-specific error code, expressed as a string value.
      Parameters:
      code - code of the error.
      Returns:
      a clone of this object, except with this updated property (returns this if an identical value is passed).
    • getTitle

      public String getTitle()
      Gets a short, human-readable summary of the problem that SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization.
      Returns:
      will never be null. -- WITHER -- Sets a short, human-readable summary of the problem that SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization.
    • withTitle

      public JsonApiError withTitle(String title)
      Gets a short, human-readable summary of the problem that SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization.
      Parameters:
      title - title of the error.
      Returns:
      a clone of this object, except with this updated property (returns this if an identical value is passed).
    • getDetail

      public String getDetail()
      Gets a human-readable explanation specific to this occurrence of the problem. Like title, the value can be localized.
      Returns:
      will never be null. -- WITHER -- Sets a human-readable explanation specific to this occurrence of the problem. Like title, the value can be localized.
    • withDetail

      public JsonApiError withDetail(String detail)
      Gets a human-readable explanation specific to this occurrence of the problem. Like title, the value can be localized.
      Parameters:
      detail - error detail.
      Returns:
      a clone of this object, except with this updated property (returns this if an identical value is passed).
    • getSource

      public Map<String,String> getSource()
      Gets an object containing references to the source of the error, optionally including any of the following members: pointer: a JSON Pointer [RFC6901] to the associated entity in the request document [e.g. "/data" for a primary data object, or "/data/attributes/title" for a specific attribute]. parameter: a string indicating which URI query parameter caused the error.
      Returns:
      can be null.
    • getMeta

      public Map<String,Object> getMeta()
      Gets a meta object containing non-standard meta-information about the error.
      Returns:
      will never be null. -- WITHER -- Sets a meta object containing non-standard meta-information about the error.
    • withMeta

      public JsonApiError withMeta(Map<String,Object> meta)
      Gets a meta object containing non-standard meta-information about the error.
      Parameters:
      meta - meta object added to the error.
      Returns:
      a clone of this object, except with this updated property (returns this if an identical value is passed).