Class ProblemDetail

java.lang.Object
org.springframework.http.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.

Since:
6.0
Author:
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).

      Parameters:
      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.

      Parameters:
      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().

      Parameters:
      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.
      Parameters:
      httpStatus - the problem status
    • setStatus

      public void setStatus(int status)
      Setter for the problem status.
      Parameters:
      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.

      Parameters:
      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.

      Parameters:
      instance - the problem instance
    • getInstance

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

      public void setProperty(String name, @Nullable 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.

      Parameters:
      name - the property name
      value - the property value, possibly null if the intent is to include a property with its value set to "null"
      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)
      Overrides:
      equals in class Object
    • hashCode

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

      public String toString()
      Overrides:
      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.