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
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
    • 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
    • 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
    • setDetail

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

      By default, this is not set.

      Parameters:
      detail - the 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
    • setProperty

      public void setProperty(String name, Object value)
      Set a "dynamic" property to be added to a generic properties map.
      Parameters:
      name - the property name
      value - the property value
    • getType

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

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

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

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

      @Nullable public URI getInstance()
      Return the configured problem instance.
    • 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).
    • 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.