Top Description Inners Methods
org.springframework.web

public Interface ErrorResponse

Known Direct Implementers
org.springframework.web.ErrorResponseException, org.springframework.web.HttpMediaTypeException, org.springframework.web.HttpRequestMethodNotSupportedException, org.springframework.web.bind.MethodArgumentNotValidException, org.springframework.web.bind.ServletRequestBindingException, org.springframework.web.context.request.async.AsyncRequestTimeoutException, org.springframework.web.multipart.MaxUploadSizeExceededException, org.springframework.web.multipart.support.MissingServletRequestPartException, org.springframework.web.DefaultErrorResponseBuilder.SimpleErrorResponse
Imports
java.net.URI, java.util.Locale, java.util.function.Consumer, org.springframework.context.MessageSource, org.springframework.http.HttpHeaders, .HttpStatusCode, .ProblemDetail, org.springframework.lang.Nullable

Representation of a complete RFC 7807 error response including status, headers, and an RFC 7807 formatted ProblemDetail body. Allows any exception to expose HTTP error response information.

ErrorResponseException is a default implementation of this interface and a convenient base class for other exceptions to use.

ErrorResponse is supported as a return value from @ExceptionHandler methods that render directly to the response, e.g. by being marked @ResponseBody, or declared in an @RestController or RestControllerAdvice class.

Author
Rossen Stoyanchev
Since
6.0
See Also
ErrorResponseException

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
ErrorResponse.Builder

Builder for an ErrorResponse.

Method Summary

Modifier and TypeMethod and Description
public static ErrorResponse.Builder
builder(Throwable
the underlying exception that lead to the error response; mainly to derive default values for the detail message code and for the title message code.
ex
,
HttpStatusCode
the status code to set in the response
statusCode
,
String
the default value for the detail field, unless overridden by a MessageSource lookup with getDetailMessageCode()
detail
)

Return a builder to create an ErrorResponse instance.

public static ErrorResponse.Builder
builder(Throwable ex, ProblemDetail problemDetail)

Variant of builder(Throwable, HttpStatusCode, String) for use with a custom ProblemDetail instance.

public static ErrorResponse
create(Throwable ex, HttpStatusCode statusCode, String detail)

Static factory method to build an instance via builder(Throwable, HttpStatusCode, String).

public ProblemDetail
getBody()

Return the body for the response, formatted as an RFC 7807 ProblemDetail whose status should match the response status.

public static String

Returns:

"problemDetail." followed by the fully qualified class name and an optional suffix
getDefaultDetailMessageCode
(Class<?>
the exception type associated with the problem
exceptionType
,
String
an optional suffix, e.g. for exceptions that may have multiple error message with different arguments
suffix
)

Build a message code for the "detail" field, for the given exception type.

public static String

Returns:

"problemDetail.title." followed by the fully qualified class name
getDefaultTitleMessageCode
(Class<?>
the exception type associated with the problem
exceptionType
)

Build a message code for the "title" field, for the given exception type.

public static String

Returns:

"problemDetail.type." followed by the fully qualified class name
getDefaultTypeMessageCode
(Class<?>
the exception type associated with the problem
exceptionType
)

Build a message code for the "type" field, for the given exception type.

public default Object[]
getDetailMessageArguments()

Return arguments to use along with a message code to resolve the problem "detail" for this exception through a MessageSource.

public default Object[]
getDetailMessageArguments(MessageSource
the MessageSource to use for the lookup
messageSource
,
Locale
the Locale to use for the lookup
locale
)

Variant of getDetailMessageArguments() that uses the given MessageSource for resolving the message argument values.

public default String
getDetailMessageCode()

Return a code to use to resolve the problem "detail" for this exception through a MessageSource.

public default HttpHeaders
getHeaders()

Return headers to use for the response.

public HttpStatusCode
getStatusCode()

Return the HTTP status code to use for the response.

public default String
getTitleMessageCode()

Return a code to use to resolve the problem "title" for this exception through a MessageSource.

public default String
getTypeMessageCode()

Return a code to use to resolve the problem "type" for this exception through a MessageSource.

public default ProblemDetail
updateAndGetBody(MessageSource
the MessageSource to use for the lookup
messageSource
,
Locale
the Locale to use for the lookup
locale
)

Use the given MessageSource to resolve the type, title, and detail message codes, and then use the resolved values to update the corresponding fields in getBody().

Method Detail

builderback to summary
public static ErrorResponse.Builder builder(Throwable ex, HttpStatusCode statusCode, String detail)

Return a builder to create an ErrorResponse instance.

Parameters
ex:Throwable

the underlying exception that lead to the error response; mainly to derive default values for the detail message code and for the title message code.

statusCode:HttpStatusCode

the status code to set in the response

detail:String

the default value for the detail field, unless overridden by a MessageSource lookup with getDetailMessageCode()

builderback to summary
public static ErrorResponse.Builder builder(Throwable ex, ProblemDetail problemDetail)

Variant of builder(Throwable, HttpStatusCode, String) for use with a custom ProblemDetail instance.

Since
6.1
createback to summary
public static ErrorResponse create(Throwable ex, HttpStatusCode statusCode, String detail)

Static factory method to build an instance via builder(Throwable, HttpStatusCode, String).

getBodyback to summary
public ProblemDetail getBody()

Return the body for the response, formatted as an RFC 7807 ProblemDetail whose status should match the response status.

getDefaultDetailMessageCodeback to summary
public static String getDefaultDetailMessageCode(Class<?> exceptionType, String suffix)

Build a message code for the "detail" field, for the given exception type.

Parameters
exceptionType:Class<?>

the exception type associated with the problem

suffix:String

an optional suffix, e.g. for exceptions that may have multiple error message with different arguments

Returns:String

"problemDetail." followed by the fully qualified class name and an optional suffix

getDefaultTitleMessageCodeback to summary
public static String getDefaultTitleMessageCode(Class<?> exceptionType)

Build a message code for the "title" field, for the given exception type.

Parameters
exceptionType:Class<?>

the exception type associated with the problem

Returns:String

"problemDetail.title." followed by the fully qualified class name

getDefaultTypeMessageCodeback to summary
public static String getDefaultTypeMessageCode(Class<?> exceptionType)

Build a message code for the "type" field, for the given exception type.

Parameters
exceptionType:Class<?>

the exception type associated with the problem

Returns:String

"problemDetail.type." followed by the fully qualified class name

Since
6.1
getDetailMessageArgumentsback to summary
public default Object[] getDetailMessageArguments()

Return arguments to use along with a message code to resolve the problem "detail" for this exception through a MessageSource. The arguments are expanded into placeholders of the message value, e.g. "Invalid content type {0}".

Annotations
@Nullable
getDetailMessageArgumentsback to summary
public default Object[] getDetailMessageArguments(MessageSource messageSource, Locale locale)

Variant of getDetailMessageArguments() that uses the given MessageSource for resolving the message argument values.

This is useful for example to expand message codes from validation errors.

The default implementation delegates to getDetailMessageArguments(), ignoring the supplied MessageSource and Locale.

Parameters
messageSource:MessageSource

the MessageSource to use for the lookup

locale:Locale

the Locale to use for the lookup

Annotations
@Nullable
getDetailMessageCodeback to summary
public default String getDetailMessageCode()

Return a code to use to resolve the problem "detail" for this exception through a MessageSource.

By default this is initialized via getDefaultDetailMessageCode(Class, String).

getHeadersback to summary
public default HttpHeaders getHeaders()

Return headers to use for the response.

getStatusCodeback to summary
public HttpStatusCode getStatusCode()

Return the HTTP status code to use for the response.

getTitleMessageCodeback to summary
public default String getTitleMessageCode()

Return a code to use to resolve the problem "title" for this exception through a MessageSource.

By default this is initialized via getDefaultTitleMessageCode(Class).

getTypeMessageCodeback to summary
public default String getTypeMessageCode()

Return a code to use to resolve the problem "type" for this exception through a MessageSource. The type resolved through the MessageSource will be passed into URI#create(String) and therefore must be an encoded URI String.

By default this is initialized via getDefaultTypeMessageCode(Class).

Since
6.1
updateAndGetBodyback to summary
public default ProblemDetail updateAndGetBody(MessageSource messageSource, Locale locale)

Use the given MessageSource to resolve the type, title, and detail message codes, and then use the resolved values to update the corresponding fields in getBody().

Parameters
messageSource:MessageSource

the MessageSource to use for the lookup

locale:Locale

the Locale to use for the lookup

org.springframework.web back to summary

public Interface ErrorResponse.Builder

Known Direct Implementers
org.springframework.web.DefaultErrorResponseBuilder

Builder for an ErrorResponse.

Method Summary

Modifier and TypeMethod and Description
public ErrorResponse
build()

Build the ErrorResponse instance.

public default ErrorResponse
build(MessageSource messageSource, Locale locale)

Build the ErrorResponse instance and also resolve the "detail" and "title" through the given MessageSource.

public ErrorResponse.Builder

Returns:

the same builder instance
detail
(String detail)

Set the underlying detail.

public ErrorResponse.Builder

Returns:

the same builder instance
detailMessageArguments
(Object...
the arguments to provide
messageArguments
)

Set the arguments to provide to the MessageSource lookup for detailMessageCode(String).

public ErrorResponse.Builder

Returns:

the same builder instance
detailMessageCode
(String
the message code to use
messageCode
)

Customize the MessageSource code to use to resolve the value for the detail(String).

public ErrorResponse.Builder

Returns:

the same builder instance
header
(String
the header name
headerName
,
String...
the header value(s)
headerValues
)

Add the given header value(s) under the given name.

public ErrorResponse.Builder

Returns:

the same builder instance
headers
(Consumer<HttpHeaders>
a function that consumes the HttpHeaders
headersConsumer
)

Manipulate this response's headers with the given consumer.

public ErrorResponse.Builder

Returns:

the same builder instance
instance
(URI instance)

Set the underlying instance field.

public ErrorResponse.Builder

Returns:

the same builder instance
property
(String name, Object value)

Set a "dynamic" property on the underlying ProblemDetail.

public ErrorResponse.Builder

Returns:

the same builder instance
title
(String title)

Set the underlying title field.

public ErrorResponse.Builder

Returns:

the same builder instance
titleMessageCode
(String
the message code to use
messageCode
)

Customize the MessageSource code to use to resolve the value for ProblemDetail#setTitle(String).

public ErrorResponse.Builder

Returns:

the same builder instance
type
(URI type)

Set the underlying type field.

public ErrorResponse.Builder

Returns:

the same builder instance
typeMessageCode
(String
the message code to use
messageCode
)

Customize the MessageSource code to use to resolve the value for ProblemDetail#setType(URI).

Method Detail

buildback to summary
public ErrorResponse build()

Build the ErrorResponse instance.

buildback to summary
public default ErrorResponse build(MessageSource messageSource, Locale locale)

Build the ErrorResponse instance and also resolve the "detail" and "title" through the given MessageSource. Effectively a shortcut for calling build() and then ErrorResponse#updateAndGetBody(MessageSource, Locale).

Since
6.0.3
detailback to summary
public ErrorResponse.Builder detail(String detail)

Set the underlying detail.

Returns:ErrorResponse.Builder

the same builder instance

detailMessageArgumentsback to summary
public ErrorResponse.Builder detailMessageArguments(Object... messageArguments)

Set the arguments to provide to the MessageSource lookup for detailMessageCode(String).

Parameters
messageArguments:Object[]

the arguments to provide

Returns:ErrorResponse.Builder

the same builder instance

See Also
ErrorResponse#getDetailMessageArguments()
detailMessageCodeback to summary
public ErrorResponse.Builder detailMessageCode(String messageCode)

Customize the MessageSource code to use to resolve the value for the detail(String).

By default, set from ErrorResponse#getDefaultDetailMessageCode(Class, String).

Parameters
messageCode:String

the message code to use

Returns:ErrorResponse.Builder

the same builder instance

See Also
ErrorResponse#getDetailMessageCode()
headerback to summary
public ErrorResponse.Builder header(String headerName, String... headerValues)

Add the given header value(s) under the given name.

Parameters
headerName:String

the header name

headerValues:String[]

the header value(s)

Returns:ErrorResponse.Builder

the same builder instance

See Also
HttpHeaders#add(String, String)
headersback to summary
public ErrorResponse.Builder headers(Consumer<HttpHeaders> headersConsumer)

Manipulate this response's headers with the given consumer. This is useful to overwrite or remove existing values, or use any other HttpHeaders methods.

Parameters
headersConsumer:Consumer<HttpHeaders>

a function that consumes the HttpHeaders

Returns:ErrorResponse.Builder

the same builder instance

instanceback to summary
public ErrorResponse.Builder instance(URI instance)

Set the underlying instance field.

Returns:ErrorResponse.Builder

the same builder instance

propertyback to summary
public ErrorResponse.Builder property(String name, Object value)

Set a "dynamic" property on the underlying ProblemDetail.

Returns:ErrorResponse.Builder

the same builder instance

titleback to summary
public ErrorResponse.Builder title(String title)

Set the underlying title field.

Returns:ErrorResponse.Builder

the same builder instance

titleMessageCodeback to summary
public ErrorResponse.Builder titleMessageCode(String messageCode)

Customize the MessageSource code to use to resolve the value for ProblemDetail#setTitle(String).

By default, set from ErrorResponse#getDefaultTitleMessageCode(Class).

Parameters
messageCode:String

the message code to use

Returns:ErrorResponse.Builder

the same builder instance

See Also
ErrorResponse#getTitleMessageCode()
typeback to summary
public ErrorResponse.Builder type(URI type)

Set the underlying type field.

Returns:ErrorResponse.Builder

the same builder instance

typeMessageCodeback to summary
public ErrorResponse.Builder typeMessageCode(String messageCode)

Customize the MessageSource code to use to resolve the value for ProblemDetail#setType(URI).

By default, set from ErrorResponse#getDefaultTypeMessageCode(Class).

Parameters
messageCode:String

the message code to use

Returns:ErrorResponse.Builder

the same builder instance

Since
6.1
See Also
ErrorResponse#getTypeMessageCode()