Class ProblemDetail

java.lang.Object
org.springframework.http.ProblemDetail

public class ProblemDetail extends Object
Representation of an RFC 7807 problem detail, including all RFC-defined fields. For an extended response with more fields, create a subclass that exposes the additional fields.
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(HttpStatus) or forRawStatusCode(int).

      Parameters:
      rawStatusCode - the response status to use
    • ProblemDetail

      protected ProblemDetail(ProblemDetail other)
      Copy constructor that could be used from a subclass to re-create a ProblemDetail in order to extend it with more fields.
  • Method Details

    • withType

      public ProblemDetail withType(URI type)
      Variant of setType(URI) for chained initialization.
      Parameters:
      type - the problem type
      Returns:
      the same instance
    • withTitle

      public ProblemDetail withTitle(@Nullable String title)
      Variant of setTitle(String) for chained initialization.
      Parameters:
      title - the problem title
      Returns:
      the same instance
    • withStatus

      public ProblemDetail withStatus(HttpStatus status)
      Variant of setStatus(int) for chained initialization.
      Parameters:
      status - the response status for the problem
      Returns:
      the same instance
    • withRawStatusCode

      public ProblemDetail withRawStatusCode(int status)
      Variant of setStatus(int) for chained initialization.
      Parameters:
      status - the response status value for the problem
      Returns:
      the same instance
    • withDetail

      public ProblemDetail withDetail(@Nullable String detail)
      Variant of setDetail(String) for chained initialization.
      Parameters:
      detail - the problem detail
      Returns:
      the same instance
    • withInstance

      public ProblemDetail withInstance(@Nullable URI instance)
      Variant of setInstance(URI) for chained initialization.
      Parameters:
      instance - the problem instance URI
      Returns:
      the same instance
    • setType

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

      By default, this is BLANK_TYPE.

      Parameters:
      type - the problem type
      See Also:
    • 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
      See Also:
    • setStatus

      public void setStatus(int status)
      Setter for the problem status.
      Parameters:
      status - the problem status
      See Also:
    • setDetail

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

      By default, this is not set.

      Parameters:
      detail - the problem detail
      See Also:
    • 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
      See Also:
    • 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.
    • 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(HttpStatus status)
      Create a ProblemDetail instance with the given status code.
    • forRawStatusCode

      public static ProblemDetail forRawStatusCode(int status)
      Create a ProblemDetail instance with the given status value.