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
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected ProblemDetailcreateProblemDetail(Exception ex, HttpStatusCode status, String defaultDetail, String detailMessageCode, Object[] detailMessageArguments, ServerWebExchange exchange) Convenience method to create aProblemDetailfor any exception that doesn't implementErrorResponse, also performing aMessageSourcelookup for the "detail" field.protected reactor.core.publisher.Mono<ResponseEntity<Object>>createResponseEntity(Object body, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Create theResponseEntityto use from the given body, headers, and statusCode.protected MessageSourceGet theMessageSourcethat 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>>handleMethodNotAllowedException(MethodNotAllowedException ex, HttpHeaders headers, HttpStatusCode status, ServerWebExchange exchange) Customize the handling ofMethodNotAllowedException.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.voidsetMessageSource(MessageSource messageSource) Set theMessageSourcethat 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:MessageSourceAwareSet theMessageSourcethat 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:
setMessageSourcein interfaceMessageSourceAware- Parameters:
messageSource- message source to be used by this object
-
getMessageSource
Get theMessageSourcethat this exception handler uses.- Since:
- 6.0.3
-
handleException
@ExceptionHandler({MethodNotAllowedException.class,NotAcceptableStatusException.class,UnsupportedMediaTypeStatusException.class,MissingRequestValueException.class,UnsatisfiedRequestParameterException.class,WebExchangeBindException.class,ServerWebInputException.class,ServerErrorException.class,ResponseStatusException.class,ErrorResponseException.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
Monowith theResponseEntityfor 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
Monowith theResponseEntityfor 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
Monowith theResponseEntityfor 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
Monowith theResponseEntityfor 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
Monowith theResponseEntityfor 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
Monowith theResponseEntityfor the response
-
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
Monowith theResponseEntityfor 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
Monowith theResponseEntityfor 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
Monowith theResponseEntityfor 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
Monowith theResponseEntityfor the response
-
createProblemDetail
protected ProblemDetail createProblemDetail(Exception ex, HttpStatusCode status, String defaultDetail, @Nullable String detailMessageCode, @Nullable Object[] detailMessageArguments, 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, falling back onErrorResponse.getDefaultDetailMessageCode(Class, String)detailMessageArguments- the arguments to go with the detailMessageCode- Returns:
- the created
ProblemDetailinstance
-
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
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 reactor.core.publisher.Mono<ResponseEntity<Object>> createResponseEntity(@Nullable Object body, @Nullable HttpHeaders headers, HttpStatusCode status, 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, e.g. 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
-