Top Description Fields Constructors Methods
org.springframework.http

public Class ProblemDetail

extends Object
Class Inheritance
Imports
java.net.URI, java.util.LinkedHashMap, .Map, .Objects, org.springframework.lang.Nullable, org.springframework.util.Assert, .ObjectUtils

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 org.springframework.http.converter.json.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 org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler or org.springframework.web.reactive.result.method.annotation.ResponseEntityExceptionHandler.

Authors
Rossen Stoyanchev, Juergen Hoeller
Since
6.0
See Also
RFC 7807, org.springframework.web.ErrorResponse, org.springframework.web.ErrorResponseException

Field Summary

Modifier and TypeField and Description
private static final URI
private String
private URI
private Map<String, Object>
private int
private String
private URI

Constructor Summary

AccessConstructor and Description
protected
ProblemDetail(int
the response status to use
rawStatusCode
)

Protected constructor for subclasses.

protected
ProblemDetail(ProblemDetail other)

Copy constructor that a subclass can use to re-create and extend a ProblemDetail with additional properties.

protected
ProblemDetail()

No-arg constructor, for deserialization.

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public static ProblemDetail
forStatus(HttpStatusCode status)

Create a ProblemDetail instance with the given status code.

public static ProblemDetail
forStatus(int status)

Create a ProblemDetail instance with the given status value.

public static ProblemDetail
forStatusAndDetail(HttpStatusCode status, String detail)

Create a ProblemDetail instance with the given status and detail.

public String
getDetail()

Return the configured problem detail.

public URI
getInstance()

Return the configured problem instance.

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.

public int
getStatus()

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

public String
getTitle()

Return the configured problem title.

public URI
getType()

Return the configured problem type.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
protected String
initToStringContent()

Return a String representation of the ProblemDetail fields.

public void
setDetail(String
the problem detail
detail
)

Setter for the problem detail.

public void
setInstance(URI
the problem instance
instance
)

Setter for the problem instance.

public void
setProperties(Map<String, Object>
the properties map
properties
)

Setter for the properties map.

public void
setProperty(String
the property name
name
,
Object
the property value, possibly null if the intent is to include a property with its value set to "null"
value
)

Set a "dynamic" property to be added to a generic properties map.

public void
setStatus(HttpStatus
the problem status
httpStatus
)

Setter for the problem status.

public void
setStatus(int
the problem status
status
)

Setter for the problem status.

public void
setTitle(String
the problem title
title
)

Setter for the problem title.

public void
setType(URI
the problem type
type
)

Setter for the problem type.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

BLANK_TYPEback to summary
private static final URI BLANK_TYPE
detailback to summary
private String detail
Annotations
@Nullable
instanceback to summary
private URI instance
Annotations
@Nullable
propertiesback to summary
private Map<String, Object> properties
Annotations
@Nullable
statusback to summary
private int status
titleback to summary
private String title
Annotations
@Nullable
typeback to summary
private URI type

Constructor Detail

ProblemDetailback to summary
protected ProblemDetail(int rawStatusCode)

Protected constructor for subclasses.

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

Parameters
rawStatusCode:int

the response status to use

ProblemDetailback to summary
protected ProblemDetail(ProblemDetail other)

Copy constructor that a subclass can use to re-create and extend a ProblemDetail with additional properties.

ProblemDetailback to summary
protected ProblemDetail()

No-arg constructor, for deserialization.

Method Detail

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
forStatusback to summary
public static ProblemDetail forStatus(HttpStatusCode status)

Create a ProblemDetail instance with the given status code.

forStatusback to summary
public static ProblemDetail forStatus(int status)

Create a ProblemDetail instance with the given status value.

forStatusAndDetailback to summary
public static ProblemDetail forStatusAndDetail(HttpStatusCode status, String detail)

Create a ProblemDetail instance with the given status and detail.

getDetailback to summary
public String getDetail()

Return the configured problem detail.

Annotations
@Nullable
getInstanceback to summary
public URI getInstance()

Return the configured problem instance.

Annotations
@Nullable
getPropertiesback to summary
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.

Annotations
@Nullable
See Also
org.springframework.http.converter.json.ProblemDetailJacksonMixin
getStatusback to summary
public int getStatus()

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

getTitleback to summary
public String getTitle()

Return the configured problem title.

Annotations
@Nullable
getTypeback to summary
public URI getType()

Return the configured problem type.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
initToStringContentback to summary
protected String initToStringContent()

Return a String representation of the ProblemDetail fields. Subclasses can override this to append additional fields.

setDetailback to summary
public void setDetail(String detail)

Setter for the problem detail.

By default, this is not set.

Parameters
detail:String

the problem detail

setInstanceback to summary
public void setInstance(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:URI

the problem instance

setPropertiesback to summary
public void setProperties(Map<String, Object> properties)

Setter for the properties map.

By default, this is not set.

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
properties:Map<String, Object>

the properties map

Since
6.0.14
setPropertyback to summary
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.

Parameters
name:String

the property name

value:Object

the property value, possibly null if the intent is to include a property with its value set to "null"

See Also
org.springframework.http.converter.json.ProblemDetailJacksonMixin
setStatusback to summary
public void setStatus(HttpStatus httpStatus)

Setter for the problem status.

Parameters
httpStatus:HttpStatus

the problem status

setStatusback to summary
public void setStatus(int status)

Setter for the problem status.

Parameters
status:int

the problem status

setTitleback to summary
public void setTitle(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:String

the problem title

setTypeback to summary
public void setType(URI type)

Setter for the problem type.

By default, this is BLANK_TYPE.

Parameters
type:URI

the problem type

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override