Class ProblemDetail


public class ProblemDetail extends Object
Representation for an RFC 7807 problem detail. Includes spec-defined properties, and a properties map for additional, non-standard properties.

For an extended response, an application can add to the properties map. When using the Jackson library, the properties map is expanded as top level JSON properties through the ProblemDetailJacksonMixin.

For an extended response, an application can also create a subclass with additional properties. Subclasses can use the protected copy constructor to re-create an existing ProblemDetail instance as the subclass, e.g. from an @ControllerAdvice such as ResponseEntityExceptionHandler.

Rossen Stoyanchev, Juergen Hoeller
See Also:
  • Constructor Details

    • ProblemDetail

      protected ProblemDetail(int rawStatusCode)
      Protected constructor for subclasses.

      To create a ProblemDetail instance, use static factory methods, forStatus(HttpStatusCode) or forStatus(int).

      rawStatusCode - the response status to use
    • ProblemDetail

      protected ProblemDetail(ProblemDetail other)
      Copy constructor that a subclass can use to re-create and extend a ProblemDetail with additional properties.
    • ProblemDetail

      protected ProblemDetail()
      No-arg constructor, for deserialization.
  • Method Details

    • setType

      public void setType(URI type)
      Setter for the problem type.

      By default, this is BLANK_TYPE.

      type - the problem type
    • getType

      public URI getType()
      Return the configured problem type.
    • setTitle

      public void setTitle(@Nullable String title)
      Setter for the problem title.

      By default, if not explicitly set and the status is well-known, this is sourced from the HttpStatus.getReasonPhrase().

      title - the problem title
    • getTitle

      @Nullable public String getTitle()
      Return the configured problem title.
    • setStatus

      public void setStatus(HttpStatus httpStatus)
      Setter for the problem status.
      httpStatus - the problem status
    • setStatus

      public void setStatus(int status)
      Setter for the problem status.
      status - the problem status
    • getStatus

      public int getStatus()
      Return the status associated with the problem, provided either to the constructor or configured via setStatus(int).
    • setDetail

      public void setDetail(@Nullable String detail)
      Setter for the problem detail.

      By default, this is not set.

      detail - the problem detail
    • getDetail

      @Nullable public String getDetail()
      Return the configured problem detail.
    • setInstance

      public void setInstance(@Nullable URI instance)
      Setter for the problem instance.

      By default, when ProblemDetail is returned from an @ExceptionHandler method, this is initialized to the request path.

      instance - the problem instance
    • getInstance

      @Nullable public URI getInstance()
      Return the configured problem instance.
    • setProperty

      public void setProperty(String name, Object value)
      Set a "dynamic" property to be added to a generic properties map.

      When Jackson JSON is present on the classpath, any properties set here are rendered as top level key-value pairs in the output JSON. Otherwise, they are rendered as a "properties" sub-map.

      name - the property name
      value - the property value
      See Also:
    • getProperties

      @Nullable public Map<String,Object> getProperties()
      Return a generic map of properties that are not known ahead of time, possibly null if no properties have been added. To add a property, use setProperty(String, Object).

      When Jackson JSON is present on the classpath, the content of this map is unwrapped and rendered as top level key-value pairs in the output JSON. Otherwise, they are rendered as a "properties" sub-map.

      See Also:
    • equals

      public boolean equals(@Nullable Object other)
      equals in class Object
    • hashCode

      public int hashCode()
      hashCode in class Object
    • toString

      public String toString()
      toString in class Object
    • initToStringContent

      protected String initToStringContent()
      Return a String representation of the ProblemDetail fields. Subclasses can override this to append additional fields.
    • forStatus

      public static ProblemDetail forStatus(HttpStatusCode status)
      Create a ProblemDetail instance with the given status code.
    • forStatus

      public static ProblemDetail forStatus(int status)
      Create a ProblemDetail instance with the given status value.
    • forStatusAndDetail

      public static ProblemDetail forStatusAndDetail(HttpStatusCode status, String detail)
      Create a ProblemDetail instance with the given status and detail.