Top Description Fields Constructors Methods
org.springframework.web.reactive.result.method.annotation

public abstract Class ResponseEntityExceptionHandler

extends Object
implements MessageSourceAware
Class Inheritance
All Implemented Interfaces
org.springframework.context.MessageSourceAware, org.springframework.beans.factory.Aware
Imports
java.util.Locale, org.apache.commons.logging.Log, .LogFactory, reactor.core.publisher.Mono, org.springframework.context.MessageSource, .MessageSourceAware, org.springframework.http.HttpHeaders, .HttpStatus, .HttpStatusCode, .ProblemDetail, .ResponseEntity, org.springframework.lang.Nullable, org.springframework.validation.method.MethodValidationException, org.springframework.web.ErrorResponse, .ErrorResponseException, org.springframework.web.bind.annotation.ControllerAdvice, .ExceptionHandler, org.springframework.web.bind.support.WebExchangeBindException, org.springframework.web.method.annotation.HandlerMethodValidationException, org.springframework.web.server.MethodNotAllowedException, .MissingRequestValueException, .NotAcceptableStatusException, .ResponseStatusException, .ServerErrorException, .ServerWebExchange, .ServerWebInputException, .UnsatisfiedRequestParameterException, .UnsupportedMediaTypeStatusException

A class with an @ExceptionHandler method that handles all Spring WebFlux raised exceptions by returning a ResponseEntity with RFC 7807 formatted error details in the body.

Convenient as a base class of an @ControllerAdvice for global exception handling in an application. Subclasses can override individual methods that handle a specific exception, override handleExceptionInternal to override common handling of all exceptions, or override createResponseEntity to intercept the final step of creating the ResponseEntity from the selected HTTP status code, headers, and body.

Author
Rossen Stoyanchev
Since
6.0

Field Summary

Modifier and TypeField and Description
protected final Log
logger

Common logger for use in subclasses.

private MessageSource

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected ProblemDetail

Returns:

the created ProblemDetail instance
createProblemDetail
(Exception
the exception being handled
ex
,
HttpStatusCode
the status to associate with the exception
status
,
String
default value for the "detail" field
defaultDetail
,
String
the code to use to look up the "detail" field through a MessageSource; if null then ErrorResponse#getDefaultDetailMessageCode(Class, String) is used to determine the default message code to use
detailMessageCode
,
Object[]
the arguments to go with the detailMessageCode
detailMessageArguments
,
ServerWebExchange exchange)

Convenience method to create a ProblemDetail for any exception that doesn't implement ErrorResponse, also performing a MessageSource lookup for the "detail" field.

protected Mono<ResponseEntity<Object>>

Returns:

a Mono with the created ResponseEntity
createResponseEntity
(Object
the body to use for the response
body
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
ServerWebExchange
the current request and response
exchange
)

Create the ResponseEntity to use from the given body, headers, and statusCode.

private static Locale
protected MessageSource
getMessageSource()

Get the MessageSource that this exception handler uses.

protected Mono<ResponseEntity<Object>>

Returns:

a Mono with the ResponseEntity for the response
handleErrorResponseException
(ErrorResponseException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
ServerWebExchange
the current request and response
exchange
)

Customize the handling of any ErrorResponseException.

public final Mono<ResponseEntity<Object>>
handleException(Exception
the exception to handle
ex
,
ServerWebExchange
the current request-response
exchange
)

Handle all exceptions raised within Spring MVC handling of the request.

protected Mono<ResponseEntity<Object>>

Returns:

a Mono with the ResponseEntity for the response
handleExceptionInternal
(Exception
the exception to handle
ex
,
Object
the body to use for the response
body
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
ServerWebExchange
the current request and response
exchange
)

Internal handler method that all others in this class delegate to, for common handling, and for the creation of a ResponseEntity.

protected Mono<ResponseEntity<Object>>

Returns:

a Mono with the ResponseEntity for the response
handleHandlerMethodValidationException
(HandlerMethodValidationException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
ServerWebExchange
the current request and response
exchange
)

Customize the handling of HandlerMethodValidationException.

protected Mono<ResponseEntity<Object>>

Returns:

a Mono with the ResponseEntity for the response
handleMethodNotAllowedException
(MethodNotAllowedException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
ServerWebExchange
the current request and response
exchange
)

Customize the handling of MethodNotAllowedException.

protected Mono<ResponseEntity<Object>>

Returns:

a Mono with the ResponseEntity for the response
handleMethodValidationException
(MethodValidationException
the exception to handle
ex
,
HttpStatus
the status code to use for the response
status
,
ServerWebExchange
the current request and response
exchange
)

Customize the handling of MethodValidationException.

protected Mono<ResponseEntity<Object>>

Returns:

a Mono with the ResponseEntity for the response
handleMissingRequestValueException
(MissingRequestValueException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
ServerWebExchange
the current request and response
exchange
)

Customize the handling of MissingRequestValueException.

protected Mono<ResponseEntity<Object>>

Returns:

a Mono with the ResponseEntity for the response
handleNotAcceptableStatusException
(NotAcceptableStatusException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
ServerWebExchange
the current request and response
exchange
)

Customize the handling of NotAcceptableStatusException.

protected Mono<ResponseEntity<Object>>

Returns:

a Mono with the ResponseEntity for the response
handleResponseStatusException
(ResponseStatusException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
ServerWebExchange
the current request and response
exchange
)

Customize the handling of any ResponseStatusException.

protected Mono<ResponseEntity<Object>>

Returns:

a Mono with the ResponseEntity for the response
handleServerErrorException
(ServerErrorException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
ServerWebExchange
the current request and response
exchange
)

Customize the handling of ServerErrorException.

protected Mono<ResponseEntity<Object>>

Returns:

a Mono with the ResponseEntity for the response
handleServerWebInputException
(ServerWebInputException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
ServerWebExchange
the current request and response
exchange
)

Customize the handling of ServerWebInputException.

protected Mono<ResponseEntity<Object>>

Returns:

a Mono with the ResponseEntity for the response
handleUnsatisfiedRequestParameterException
(UnsatisfiedRequestParameterException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
ServerWebExchange
the current request and response
exchange
)

Customize the handling of UnsatisfiedRequestParameterException.

protected Mono<ResponseEntity<Object>>

Returns:

a Mono with the ResponseEntity for the response
handleUnsupportedMediaTypeStatusException
(UnsupportedMediaTypeStatusException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
ServerWebExchange
the current request and response
exchange
)

Customize the handling of UnsupportedMediaTypeStatusException.

protected Mono<ResponseEntity<Object>>

Returns:

a Mono with the ResponseEntity for the response
handleWebExchangeBindException
(WebExchangeBindException
the exception to handle
ex
,
HttpHeaders
the headers to use for the response
headers
,
HttpStatusCode
the status code to use for the response
status
,
ServerWebExchange
the current request and response
exchange
)

Customize the handling of WebExchangeBindException.

public void
setMessageSource(MessageSource
message source to be used by this object
messageSource
)

Implements org.springframework.context.MessageSourceAware.setMessageSource.

Set the MessageSource that this object runs in.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

loggerback to summary
protected final Log logger

Common logger for use in subclasses.

messageSourceback to summary
private MessageSource messageSource
Annotations
@Nullable

Constructor Detail

ResponseEntityExceptionHandlerback to summary
public ResponseEntityExceptionHandler()

Method Detail

createProblemDetailback to summary
protected ProblemDetail createProblemDetail(Exception ex, HttpStatusCode status, String defaultDetail, String detailMessageCode, Object[] detailMessageArguments, ServerWebExchange exchange)

Convenience method to create a ProblemDetail for any exception that doesn't implement ErrorResponse, also performing a MessageSource lookup for the "detail" field.

Parameters
ex:Exception

the exception being handled

status:HttpStatusCode

the status to associate with the exception

defaultDetail:String

default value for the "detail" field

detailMessageCode:String

the code to use to look up the "detail" field through a MessageSource; if null then ErrorResponse#getDefaultDetailMessageCode(Class, String) is used to determine the default message code to use

detailMessageArguments:Object[]

the arguments to go with the detailMessageCode

Returns:ProblemDetail

the created ProblemDetail instance

createResponseEntityback to summary
protected Mono<ResponseEntity<Object>> createResponseEntity(Object body, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange)

Create the ResponseEntity to use from the given body, headers, and statusCode. Subclasses can override this method to inspect and possibly modify the body, headers, or statusCode, e.g. to re-create an instance of ProblemDetail as an extension of ProblemDetail.

Parameters
body:Object

the body to use for the response

headers:HttpHeaders

the headers to use for the response

status:HttpStatusCode

the status code to use for the response

exchange:ServerWebExchange

the current request and response

Returns:Mono<ResponseEntity<Object>>

a Mono with the created ResponseEntity

Since
6.0
getLocaleback to summary
private static Locale getLocale(ServerWebExchange exchange)
getMessageSourceback to summary
protected MessageSource getMessageSource()

Get the MessageSource that this exception handler uses.

Annotations
@Nullable
Since
6.0.3
handleErrorResponseExceptionback to summary
protected Mono<ResponseEntity<Object>> handleErrorResponseException(ErrorResponseException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange)

Customize the handling of any ErrorResponseException.

This method delegates to handleExceptionInternal.

Parameters
ex:ErrorResponseException

the exception to handle

headers:HttpHeaders

the headers to use for the response

status:HttpStatusCode

the status code to use for the response

exchange:ServerWebExchange

the current request and response

Returns:Mono<ResponseEntity<Object>>

a Mono with the ResponseEntity for the response

handleExceptionback to summary
public final Mono<ResponseEntity<Object>> handleException(Exception ex, ServerWebExchange exchange)

Handle all exceptions raised within Spring MVC handling of the request.

Parameters
ex:Exception

the exception to handle

exchange:ServerWebExchange

the current request-response

Annotations
@ExceptionHandler:MethodNotAllowedException, NotAcceptableStatusException, UnsupportedMediaTypeStatusException, MissingRequestValueException, UnsatisfiedRequestParameterException, WebExchangeBindException, HandlerMethodValidationException, ServerWebInputException, ServerErrorException, ResponseStatusException, ErrorResponseException, MethodValidationException
handleExceptionInternalback to summary
protected Mono<ResponseEntity<Object>> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange)

Internal handler method that all others in this class delegate to, for common handling, and for the creation of a ResponseEntity.

The default implementation does the following:

  • return null if response is already committed
  • set the "jakarta.servlet.error.exception" request attribute if the response status is 500 (INTERNAL_SERVER_ERROR).
  • extract the body from ErrorResponse exceptions, if the body is null.
Parameters
ex:Exception

the exception to handle

body:Object

the body to use for the response

headers:HttpHeaders

the headers to use for the response

status:HttpStatusCode

the status code to use for the response

exchange:ServerWebExchange

the current request and response

Returns:Mono<ResponseEntity<Object>>

a Mono with the ResponseEntity for the response

handleHandlerMethodValidationExceptionback to summary
protected Mono<ResponseEntity<Object>> handleHandlerMethodValidationException(HandlerMethodValidationException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange)

Customize the handling of HandlerMethodValidationException.

This method delegates to handleExceptionInternal.

Parameters
ex:HandlerMethodValidationException

the exception to handle

headers:HttpHeaders

the headers to use for the response

status:HttpStatusCode

the status code to use for the response

exchange:ServerWebExchange

the current request and response

Returns:Mono<ResponseEntity<Object>>

a Mono with the ResponseEntity for the response

Since
6.1
handleMethodNotAllowedExceptionback to summary
protected Mono<ResponseEntity<Object>> handleMethodNotAllowedException(MethodNotAllowedException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange)

Customize the handling of MethodNotAllowedException.

This method delegates to handleExceptionInternal.

Parameters
ex:MethodNotAllowedException

the exception to handle

headers:HttpHeaders

the headers to use for the response

status:HttpStatusCode

the status code to use for the response

exchange:ServerWebExchange

the current request and response

Returns:Mono<ResponseEntity<Object>>

a Mono with the ResponseEntity for the response

handleMethodValidationExceptionback to summary
protected Mono<ResponseEntity<Object>> handleMethodValidationException(MethodValidationException ex, HttpStatus status, ServerWebExchange exchange)

Customize the handling of MethodValidationException.

This method delegates to handleExceptionInternal.

Parameters
ex:MethodValidationException

the exception to handle

status:HttpStatus

the status code to use for the response

exchange:ServerWebExchange

the current request and response

Returns:Mono<ResponseEntity<Object>>

a Mono with the ResponseEntity for the response

Since
6.1
handleMissingRequestValueExceptionback to summary
protected Mono<ResponseEntity<Object>> handleMissingRequestValueException(MissingRequestValueException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange)

Customize the handling of MissingRequestValueException.

This method delegates to handleExceptionInternal.

Parameters
ex:MissingRequestValueException

the exception to handle

headers:HttpHeaders

the headers to use for the response

status:HttpStatusCode

the status code to use for the response

exchange:ServerWebExchange

the current request and response

Returns:Mono<ResponseEntity<Object>>

a Mono with the ResponseEntity for the response

handleNotAcceptableStatusExceptionback to summary
protected Mono<ResponseEntity<Object>> handleNotAcceptableStatusException(NotAcceptableStatusException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange)

Customize the handling of NotAcceptableStatusException.

This method delegates to handleExceptionInternal.

Parameters
ex:NotAcceptableStatusException

the exception to handle

headers:HttpHeaders

the headers to use for the response

status:HttpStatusCode

the status code to use for the response

exchange:ServerWebExchange

the current request and response

Returns:Mono<ResponseEntity<Object>>

a Mono with the ResponseEntity for the response

handleResponseStatusExceptionback to summary
protected Mono<ResponseEntity<Object>> handleResponseStatusException(ResponseStatusException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange)

Customize the handling of any ResponseStatusException.

This method delegates to handleExceptionInternal.

Parameters
ex:ResponseStatusException

the exception to handle

headers:HttpHeaders

the headers to use for the response

status:HttpStatusCode

the status code to use for the response

exchange:ServerWebExchange

the current request and response

Returns:Mono<ResponseEntity<Object>>

a Mono with the ResponseEntity for the response

handleServerErrorExceptionback to summary
protected Mono<ResponseEntity<Object>> handleServerErrorException(ServerErrorException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange)

Customize the handling of ServerErrorException.

This method delegates to handleExceptionInternal.

Parameters
ex:ServerErrorException

the exception to handle

headers:HttpHeaders

the headers to use for the response

status:HttpStatusCode

the status code to use for the response

exchange:ServerWebExchange

the current request and response

Returns:Mono<ResponseEntity<Object>>

a Mono with the ResponseEntity for the response

handleServerWebInputExceptionback to summary
protected Mono<ResponseEntity<Object>> handleServerWebInputException(ServerWebInputException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange)

Customize the handling of ServerWebInputException.

This method delegates to handleExceptionInternal.

Parameters
ex:ServerWebInputException

the exception to handle

headers:HttpHeaders

the headers to use for the response

status:HttpStatusCode

the status code to use for the response

exchange:ServerWebExchange

the current request and response

Returns:Mono<ResponseEntity<Object>>

a Mono with the ResponseEntity for the response

handleUnsatisfiedRequestParameterExceptionback to summary
protected Mono<ResponseEntity<Object>> handleUnsatisfiedRequestParameterException(UnsatisfiedRequestParameterException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange)

Customize the handling of UnsatisfiedRequestParameterException.

This method delegates to handleExceptionInternal.

Parameters
ex:UnsatisfiedRequestParameterException

the exception to handle

headers:HttpHeaders

the headers to use for the response

status:HttpStatusCode

the status code to use for the response

exchange:ServerWebExchange

the current request and response

Returns:Mono<ResponseEntity<Object>>

a Mono with the ResponseEntity for the response

handleUnsupportedMediaTypeStatusExceptionback to summary
protected Mono<ResponseEntity<Object>> handleUnsupportedMediaTypeStatusException(UnsupportedMediaTypeStatusException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange)

Customize the handling of UnsupportedMediaTypeStatusException.

This method delegates to handleExceptionInternal.

Parameters
ex:UnsupportedMediaTypeStatusException

the exception to handle

headers:HttpHeaders

the headers to use for the response

status:HttpStatusCode

the status code to use for the response

exchange:ServerWebExchange

the current request and response

Returns:Mono<ResponseEntity<Object>>

a Mono with the ResponseEntity for the response

handleWebExchangeBindExceptionback to summary
protected Mono<ResponseEntity<Object>> handleWebExchangeBindException(WebExchangeBindException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange)

Customize the handling of WebExchangeBindException.

This method delegates to handleExceptionInternal.

Parameters
ex:WebExchangeBindException

the exception to handle

headers:HttpHeaders

the headers to use for the response

status:HttpStatusCode

the status code to use for the response

exchange:ServerWebExchange

the current request and response

Returns:Mono<ResponseEntity<Object>>

a Mono with the ResponseEntity for the response

setMessageSourceback to summary
public void setMessageSource(MessageSource messageSource)

Implements org.springframework.context.MessageSourceAware.setMessageSource.

Doc from org.springframework.context.MessageSourceAware.setMessageSource.

Set the MessageSource that this object runs in.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method. Invoked before ApplicationContextAware's setApplicationContext.

Parameters
messageSource:MessageSource

message source to be used by this object

Annotations
@Override