Package org.springframework.http

# Class ProblemDetail

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

All Implemented Interfaces:
  `Serializable`

public class ProblemDetail extends Object implements Serializable
Representation for an RFC 9457 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, for example, from an `@ControllerAdvice` such as `org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler` or `org.springframework.web.reactive.result.method.annotation.ResponseEntityExceptionHandler`.

Since:
  6.0
Author:
  Rossen Stoyanchev, Juergen Hoeller
See Also:
  - RFC 9457
  - `ErrorResponse`
  - `ErrorResponseException`
  - Serialized Form

## Constructor Summary

Constructors
Modifier
Constructor
Description
`protected`
`ProblemDetail()`
No-arg constructor, for deserialization.
`protected`
`ProblemDetail(int 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.

## Method Summary

Modifier and Type
Method
Description
`boolean`
`equals(Object other)`
`static ProblemDetail`
`forStatus(int status)`
Create a `ProblemDetail` instance with the given status value.
`static ProblemDetail`
`forStatus(HttpStatusCode status)`
Create a `ProblemDetail` instance with the given status code.
`static ProblemDetail`
`forStatusAndDetail(HttpStatusCode status, String detail)`
Create a `ProblemDetail` instance with the given status and detail.
`String`
`getDetail()`
Return the configured `problem detail`.
`URI`
`getInstance()`
Return the configured `problem instance`.
`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.
`int`
`getStatus()`
Return the status associated with the problem, provided either to the constructor or configured via `setStatus(int)`.
`String`
`getTitle()`
Return the configured `problem title`.
`URI`
`getType()`
Return the configured `problem type`.
`int`
`hashCode()`
`protected String`
`initToStringContent()`
Return a String representation of the `ProblemDetail` fields.
`void`
`setDetail(String detail)`
Setter for the `problem detail`.
`void`
`setInstance(URI instance)`
Setter for the `problem instance`.
`void`
`setProperties(Map<String,Object> properties)`
Setter for the `properties map`.
`void`
`setProperty(String name, Object value)`
Set a "dynamic" property to be added to a generic `properties map`.
`void`
`setStatus(int status)`
Setter for the `problem status`.
`void`
`setStatus(HttpStatus httpStatus)`
Setter for the `problem status`.
`void`
`setTitle(String title)`
Setter for the `problem title`.
`void`
`setType(URI type)`
Setter for the `problem type`.
`String`
`toString()`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

## 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:
  - `ProblemDetailJacksonMixin`

### setProperties

public void setProperties(@Nullable 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` - the properties map
Since:
  6.0.14

### 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:
  - `ProblemDetailJacksonMixin`

### 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, @Nullable String detail)
Create a `ProblemDetail` instance with the given status and detail.