Class ResponseEntityExceptionHandler
- All Implemented Interfaces:
Aware
,MessageSourceAware
@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(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.server.ServerWebExchange)
to override common handling of all exceptions,
or override createResponseEntity(java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.server.ServerWebExchange)
to intercept the final step of creating
the ResponseEntity
from the selected HTTP status code, headers, and body.
- Since:
- 6.0
- Author:
- Rossen Stoyanchev
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected ProblemDetail
createProblemDetail
(Exception ex, HttpStatusCode status, String defaultDetail, String detailMessageCode, Object[] detailMessageArguments, ServerWebExchange exchange) Convenience method to create aProblemDetail
for any exception that doesn't implementErrorResponse
, also performing aMessageSource
lookup for the "detail" field.protected reactor.core.publisher.Mono<ResponseEntity<Object>>
createResponseEntity
(Object body, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Create theResponseEntity
to use from the given body, headers, and statusCode.protected MessageSource
Get theMessageSource
that this exception handler uses.protected reactor.core.publisher.Mono<ResponseEntity<Object>>
handleErrorResponseException
(ErrorResponseException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling of anyErrorResponseException
.final reactor.core.publisher.Mono<ResponseEntity<Object>>
handleException
(Exception ex, ServerWebExchange exchange) Handle all exceptions raised within Spring MVC handling of the request.protected reactor.core.publisher.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 aResponseEntity
.protected reactor.core.publisher.Mono<ResponseEntity<Object>>
handleHandlerMethodValidationException
(HandlerMethodValidationException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofHandlerMethodValidationException
.protected reactor.core.publisher.Mono<ResponseEntity<Object>>
handleMethodNotAllowedException
(MethodNotAllowedException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofMethodNotAllowedException
.protected reactor.core.publisher.Mono<ResponseEntity<Object>>
handleMethodValidationException
(MethodValidationException ex, HttpStatus status, ServerWebExchange exchange) Customize the handling ofMethodValidationException
.protected reactor.core.publisher.Mono<ResponseEntity<Object>>
handleMissingRequestValueException
(MissingRequestValueException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofMissingRequestValueException
.protected reactor.core.publisher.Mono<ResponseEntity<Object>>
handleNotAcceptableStatusException
(NotAcceptableStatusException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofNotAcceptableStatusException
.protected reactor.core.publisher.Mono<ResponseEntity<Object>>
handleResponseStatusException
(ResponseStatusException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling of anyResponseStatusException
.protected reactor.core.publisher.Mono<ResponseEntity<Object>>
handleServerErrorException
(ServerErrorException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofServerErrorException
.protected reactor.core.publisher.Mono<ResponseEntity<Object>>
handleServerWebInputException
(ServerWebInputException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofServerWebInputException
.protected reactor.core.publisher.Mono<ResponseEntity<Object>>
handleUnsatisfiedRequestParameterException
(UnsatisfiedRequestParameterException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofUnsatisfiedRequestParameterException
.protected reactor.core.publisher.Mono<ResponseEntity<Object>>
handleUnsupportedMediaTypeStatusException
(UnsupportedMediaTypeStatusException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofUnsupportedMediaTypeStatusException
.protected reactor.core.publisher.Mono<ResponseEntity<Object>>
handleWebExchangeBindException
(WebExchangeBindException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofWebExchangeBindException
.void
setMessageSource
(MessageSource messageSource) Set theMessageSource
that this object runs in.
-
Field Details
-
logger
Common logger for use in subclasses.
-
-
Constructor Details
-
ResponseEntityExceptionHandler
public ResponseEntityExceptionHandler()
-
-
Method Details
-
setMessageSource
Description copied from interface:MessageSourceAware
Set theMessageSource
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.
- Specified by:
setMessageSource
in interfaceMessageSourceAware
- Parameters:
messageSource
- message source to be used by this object
-
getMessageSource
Get theMessageSource
that this exception handler uses.- Since:
- 6.0.3
-
handleException
@ExceptionHandler({MethodNotAllowedException.class,NotAcceptableStatusException.class,UnsupportedMediaTypeStatusException.class,MissingRequestValueException.class,UnsatisfiedRequestParameterException.class,WebExchangeBindException.class,HandlerMethodValidationException.class,ServerWebInputException.class,ServerErrorException.class,ResponseStatusException.class,ErrorResponseException.class,MethodValidationException.class}) public final reactor.core.publisher.Mono<ResponseEntity<Object>> handleException(Exception ex, ServerWebExchange exchange) Handle all exceptions raised within Spring MVC handling of the request.- Parameters:
ex
- the exception to handleexchange
- the current request-response
-
handleMethodNotAllowedException
protected reactor.core.publisher.Mono<ResponseEntity<Object>> handleMethodNotAllowedException(MethodNotAllowedException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofMethodNotAllowedException
.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.server.ServerWebExchange)
.- Parameters:
ex
- the exception to handleheaders
- the headers to use for the responsestatus
- the status code to use for the responseexchange
- the current request and response- Returns:
- a
Mono
with theResponseEntity
for the response
-
handleNotAcceptableStatusException
protected reactor.core.publisher.Mono<ResponseEntity<Object>> handleNotAcceptableStatusException(NotAcceptableStatusException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofNotAcceptableStatusException
.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.server.ServerWebExchange)
.- Parameters:
ex
- the exception to handleheaders
- the headers to use for the responsestatus
- the status code to use for the responseexchange
- the current request and response- Returns:
- a
Mono
with theResponseEntity
for the response
-
handleUnsupportedMediaTypeStatusException
protected reactor.core.publisher.Mono<ResponseEntity<Object>> handleUnsupportedMediaTypeStatusException(UnsupportedMediaTypeStatusException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofUnsupportedMediaTypeStatusException
.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.server.ServerWebExchange)
.- Parameters:
ex
- the exception to handleheaders
- the headers to use for the responsestatus
- the status code to use for the responseexchange
- the current request and response- Returns:
- a
Mono
with theResponseEntity
for the response
-
handleMissingRequestValueException
protected reactor.core.publisher.Mono<ResponseEntity<Object>> handleMissingRequestValueException(MissingRequestValueException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofMissingRequestValueException
.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.server.ServerWebExchange)
.- Parameters:
ex
- the exception to handleheaders
- the headers to use for the responsestatus
- the status code to use for the responseexchange
- the current request and response- Returns:
- a
Mono
with theResponseEntity
for the response
-
handleUnsatisfiedRequestParameterException
protected reactor.core.publisher.Mono<ResponseEntity<Object>> handleUnsatisfiedRequestParameterException(UnsatisfiedRequestParameterException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofUnsatisfiedRequestParameterException
.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.server.ServerWebExchange)
.- Parameters:
ex
- the exception to handleheaders
- the headers to use for the responsestatus
- the status code to use for the responseexchange
- the current request and response- Returns:
- a
Mono
with theResponseEntity
for the response
-
handleWebExchangeBindException
protected reactor.core.publisher.Mono<ResponseEntity<Object>> handleWebExchangeBindException(WebExchangeBindException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofWebExchangeBindException
.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.server.ServerWebExchange)
.- Parameters:
ex
- the exception to handleheaders
- the headers to use for the responsestatus
- the status code to use for the responseexchange
- the current request and response- Returns:
- a
Mono
with theResponseEntity
for the response
-
handleHandlerMethodValidationException
protected reactor.core.publisher.Mono<ResponseEntity<Object>> handleHandlerMethodValidationException(HandlerMethodValidationException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofHandlerMethodValidationException
.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.server.ServerWebExchange)
.- Parameters:
ex
- the exception to handleheaders
- the headers to use for the responsestatus
- the status code to use for the responseexchange
- the current request and response- Returns:
- a
Mono
with theResponseEntity
for the response - Since:
- 6.1
-
handleServerWebInputException
protected reactor.core.publisher.Mono<ResponseEntity<Object>> handleServerWebInputException(ServerWebInputException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofServerWebInputException
.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.server.ServerWebExchange)
.- Parameters:
ex
- the exception to handleheaders
- the headers to use for the responsestatus
- the status code to use for the responseexchange
- the current request and response- Returns:
- a
Mono
with theResponseEntity
for the response
-
handleResponseStatusException
protected reactor.core.publisher.Mono<ResponseEntity<Object>> handleResponseStatusException(ResponseStatusException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling of anyResponseStatusException
.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.server.ServerWebExchange)
.- Parameters:
ex
- the exception to handleheaders
- the headers to use for the responsestatus
- the status code to use for the responseexchange
- the current request and response- Returns:
- a
Mono
with theResponseEntity
for the response
-
handleServerErrorException
protected reactor.core.publisher.Mono<ResponseEntity<Object>> handleServerErrorException(ServerErrorException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofServerErrorException
.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.server.ServerWebExchange)
.- Parameters:
ex
- the exception to handleheaders
- the headers to use for the responsestatus
- the status code to use for the responseexchange
- the current request and response- Returns:
- a
Mono
with theResponseEntity
for the response
-
handleErrorResponseException
protected reactor.core.publisher.Mono<ResponseEntity<Object>> handleErrorResponseException(ErrorResponseException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling of anyErrorResponseException
.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.server.ServerWebExchange)
.- Parameters:
ex
- the exception to handleheaders
- the headers to use for the responsestatus
- the status code to use for the responseexchange
- the current request and response- Returns:
- a
Mono
with theResponseEntity
for the response
-
handleMethodValidationException
protected reactor.core.publisher.Mono<ResponseEntity<Object>> handleMethodValidationException(MethodValidationException ex, HttpStatus status, ServerWebExchange exchange) Customize the handling ofMethodValidationException
.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.server.ServerWebExchange)
.- Parameters:
ex
- the exception to handlestatus
- the status code to use for the responseexchange
- the current request and response- Returns:
- a
Mono
with theResponseEntity
for the response - Since:
- 6.1
-
createProblemDetail
protected ProblemDetail createProblemDetail(Exception ex, HttpStatusCode status, String defaultDetail, @Nullable String detailMessageCode, @Nullable Object[] detailMessageArguments, ServerWebExchange exchange) Convenience method to create aProblemDetail
for any exception that doesn't implementErrorResponse
, also performing aMessageSource
lookup for the "detail" field.- Parameters:
ex
- the exception being handledstatus
- the status to associate with the exceptiondefaultDetail
- default value for the "detail" fielddetailMessageCode
- the code to use to look up the "detail" field through aMessageSource
; ifnull
thenErrorResponse.getDefaultDetailMessageCode(Class, String)
is used to determine the default message code to usedetailMessageArguments
- the arguments to go with the detailMessageCode- Returns:
- the created
ProblemDetail
instance
-
handleExceptionInternal
protected reactor.core.publisher.Mono<ResponseEntity<Object>> handleExceptionInternal(Exception ex, @Nullable Object body, @Nullable 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 aResponseEntity
.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
fromErrorResponse
exceptions, if thebody
isnull
.
- Parameters:
ex
- the exception to handlebody
- the body to use for the responseheaders
- the headers to use for the responsestatus
- the status code to use for the responseexchange
- the current request and response- Returns:
- a
Mono
with theResponseEntity
for the response
- return
-
createResponseEntity
protected reactor.core.publisher.Mono<ResponseEntity<Object>> createResponseEntity(@Nullable Object body, @Nullable HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Create theResponseEntity
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 ofProblemDetail
as an extension ofProblemDetail
.- Parameters:
body
- the body to use for the responseheaders
- the headers to use for the responsestatus
- the status code to use for the responseexchange
- the current request and response- Returns:
- a
Mono
with the createdResponseEntity
- Since:
- 6.0
-