Class ResponseEntityExceptionHandler
- All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.context.MessageSourceAware
@ExceptionHandler method that handles all Spring
WebFlux raised exceptions by returning a ResponseEntity with
RFC 9457 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(Exception, Object, HttpHeaders, HttpStatusCode, ServerWebExchange) to override common handling of all exceptions,
or override createResponseEntity(Object, HttpHeaders, HttpStatusCode, 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
FieldsModifier and TypeFieldDescriptionprotected final org.apache.commons.logging.LogCommon logger for use in subclasses. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected org.springframework.http.ProblemDetailcreateProblemDetail(Exception ex, org.springframework.http.HttpStatusCode status, String defaultDetail, @Nullable String detailMessageCode, Object @Nullable [] detailMessageArguments, org.springframework.web.server.ServerWebExchange exchange) Convenience method to create aProblemDetailfor any exception that doesn't implementErrorResponse, also performing aMessageSourcelookup for the "detail" field.createResponseEntity(@Nullable Object body, @Nullable org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Create theResponseEntityto use from the given body, headers, and statusCode.protected @Nullable org.springframework.context.MessageSourceGet theMessageSourcethat this exception handler uses.handleErrorResponseException(org.springframework.web.ErrorResponseException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling of anyErrorResponseException.handleException(Exception ex, org.springframework.web.server.ServerWebExchange exchange) Handle all exceptions raised within Spring WebFlux handling of the request.handleExceptionInternal(Exception ex, @Nullable Object body, @Nullable org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Internal handler method that all others in this class delegate to, for common handling, and for the creation of aResponseEntity.handleHandlerMethodValidationException(org.springframework.web.method.annotation.HandlerMethodValidationException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofHandlerMethodValidationException.handleMethodNotAllowedException(org.springframework.web.server.MethodNotAllowedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofMethodNotAllowedException.handleMethodValidationException(org.springframework.validation.method.MethodValidationException ex, org.springframework.http.HttpStatus status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofMethodValidationException.handleMissingRequestValueException(org.springframework.web.server.MissingRequestValueException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofMissingRequestValueException.handleNotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofNotAcceptableStatusException.handleResponseStatusException(org.springframework.web.server.ResponseStatusException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling of anyResponseStatusException.handleServerErrorException(org.springframework.web.server.ServerErrorException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofServerErrorException.handleServerWebInputException(org.springframework.web.server.ServerWebInputException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofServerWebInputException.handleUnsatisfiedRequestParameterException(org.springframework.web.server.UnsatisfiedRequestParameterException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofUnsatisfiedRequestParameterException.handleUnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofUnsupportedMediaTypeStatusException.handleWebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofWebExchangeBindException.voidsetMessageSource(org.springframework.context.MessageSource messageSource)
-
Field Details
-
logger
protected final org.apache.commons.logging.Log loggerCommon logger for use in subclasses.
-
-
Constructor Details
-
ResponseEntityExceptionHandler
public ResponseEntityExceptionHandler()
-
-
Method Details
-
setMessageSource
public void setMessageSource(org.springframework.context.MessageSource messageSource) - Specified by:
setMessageSourcein interfaceorg.springframework.context.MessageSourceAware
-
getMessageSource
Get theMessageSourcethat this exception handler uses.- Since:
- 6.0.3
-
handleException
@ExceptionHandler({org.springframework.web.server.MethodNotAllowedException.class,org.springframework.web.server.NotAcceptableStatusException.class,org.springframework.web.server.UnsupportedMediaTypeStatusException.class,org.springframework.web.server.MissingRequestValueException.class,org.springframework.web.server.UnsatisfiedRequestParameterException.class,org.springframework.web.bind.support.WebExchangeBindException.class,org.springframework.web.method.annotation.HandlerMethodValidationException.class,org.springframework.web.server.ServerWebInputException.class,org.springframework.web.server.ServerErrorException.class,org.springframework.web.server.ResponseStatusException.class,org.springframework.web.ErrorResponseException.class,org.springframework.validation.method.MethodValidationException.class}) public final Mono<org.springframework.http.ResponseEntity<Object>> handleException(Exception ex, org.springframework.web.server.ServerWebExchange exchange) Handle all exceptions raised within Spring WebFlux handling of the request.- Parameters:
ex- the exception to handleexchange- the current request-response
-
handleMethodNotAllowedException
protected Mono<org.springframework.http.ResponseEntity<Object>> handleMethodNotAllowedException(org.springframework.web.server.MethodNotAllowedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofMethodNotAllowedException.This method delegates to
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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
Monowith theResponseEntityfor the response
-
handleNotAcceptableStatusException
protected Mono<org.springframework.http.ResponseEntity<Object>> handleNotAcceptableStatusException(org.springframework.web.server.NotAcceptableStatusException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofNotAcceptableStatusException.This method delegates to
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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
Monowith theResponseEntityfor the response
-
handleUnsupportedMediaTypeStatusException
protected Mono<org.springframework.http.ResponseEntity<Object>> handleUnsupportedMediaTypeStatusException(org.springframework.web.server.UnsupportedMediaTypeStatusException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofUnsupportedMediaTypeStatusException.This method delegates to
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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
Monowith theResponseEntityfor the response
-
handleMissingRequestValueException
protected Mono<org.springframework.http.ResponseEntity<Object>> handleMissingRequestValueException(org.springframework.web.server.MissingRequestValueException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofMissingRequestValueException.This method delegates to
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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
Monowith theResponseEntityfor the response
-
handleUnsatisfiedRequestParameterException
protected Mono<org.springframework.http.ResponseEntity<Object>> handleUnsatisfiedRequestParameterException(org.springframework.web.server.UnsatisfiedRequestParameterException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofUnsatisfiedRequestParameterException.This method delegates to
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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
Monowith theResponseEntityfor the response
-
handleWebExchangeBindException
protected Mono<org.springframework.http.ResponseEntity<Object>> handleWebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofWebExchangeBindException.This method delegates to
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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
Monowith theResponseEntityfor the response
-
handleHandlerMethodValidationException
protected Mono<org.springframework.http.ResponseEntity<Object>> handleHandlerMethodValidationException(org.springframework.web.method.annotation.HandlerMethodValidationException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofHandlerMethodValidationException.This method delegates to
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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
Monowith theResponseEntityfor the response - Since:
- 6.1
-
handleServerWebInputException
protected Mono<org.springframework.http.ResponseEntity<Object>> handleServerWebInputException(org.springframework.web.server.ServerWebInputException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofServerWebInputException.This method delegates to
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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
Monowith theResponseEntityfor the response
-
handleResponseStatusException
protected Mono<org.springframework.http.ResponseEntity<Object>> handleResponseStatusException(org.springframework.web.server.ResponseStatusException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling of anyResponseStatusException.This method delegates to
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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
Monowith theResponseEntityfor the response
-
handleServerErrorException
protected Mono<org.springframework.http.ResponseEntity<Object>> handleServerErrorException(org.springframework.web.server.ServerErrorException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofServerErrorException.This method delegates to
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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
Monowith theResponseEntityfor the response
-
handleErrorResponseException
protected Mono<org.springframework.http.ResponseEntity<Object>> handleErrorResponseException(org.springframework.web.ErrorResponseException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling of anyErrorResponseException.This method delegates to
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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
Monowith theResponseEntityfor the response
-
handleMethodValidationException
protected Mono<org.springframework.http.ResponseEntity<Object>> handleMethodValidationException(org.springframework.validation.method.MethodValidationException ex, org.springframework.http.HttpStatus status, org.springframework.web.server.ServerWebExchange exchange) Customize the handling ofMethodValidationException.This method delegates to
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, ServerWebExchange).- Parameters:
ex- the exception to handlestatus- the status code to use for the responseexchange- the current request and response- Returns:
- a
Monowith theResponseEntityfor the response - Since:
- 6.1
-
createProblemDetail
protected org.springframework.http.ProblemDetail createProblemDetail(Exception ex, org.springframework.http.HttpStatusCode status, String defaultDetail, @Nullable String detailMessageCode, Object @Nullable [] detailMessageArguments, org.springframework.web.server.ServerWebExchange exchange) Convenience method to create aProblemDetailfor any exception that doesn't implementErrorResponse, also performing aMessageSourcelookup 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; ifnullthenErrorResponse.getDefaultDetailMessageCode(Class, String)is used to determine the default message code to usedetailMessageArguments- the arguments to go with the detailMessageCode- Returns:
- the created
ProblemDetailinstance
-
handleExceptionInternal
protected Mono<org.springframework.http.ResponseEntity<Object>> handleExceptionInternal(Exception ex, @Nullable Object body, @Nullable org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.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
nullif response is already committed - set the
"jakarta.servlet.error.exception"request attribute if the response status is 500 (INTERNAL_SERVER_ERROR). - extract the
bodyfromErrorResponseexceptions, if thebodyisnull.
- 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
Monowith theResponseEntityfor the response
- return
-
createResponseEntity
protected Mono<org.springframework.http.ResponseEntity<Object>> createResponseEntity(@Nullable Object body, @Nullable org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange) Create theResponseEntityto use from the given body, headers, and statusCode. Subclasses can override this method to inspect and possibly modify the body, headers, or statusCode, for example, to re-create an instance ofProblemDetailas 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
Monowith the createdResponseEntity - Since:
- 6.0
-