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 SummaryFields
- 
Constructor SummaryConstructors
- 
Method SummaryModifier 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- 
loggerCommon logger for use in subclasses.
 
- 
- 
Constructor Details- 
ResponseEntityExceptionHandlerpublic ResponseEntityExceptionHandler()
 
- 
- 
Method Details- 
setMessageSourceDescription 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 interface- MessageSourceAware
- Parameters:
- messageSource- message source to be used by this object
 
- 
getMessageSourceGet 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 handle
- exchange- the current request-response
 
- 
handleMethodNotAllowedExceptionprotected 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 handle
- headers- the headers to use for the response
- status- the status code to use for the response
- exchange- the current request and response
- Returns:
- a Monowith theResponseEntityfor the response
 
- 
handleNotAcceptableStatusExceptionprotected 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 handle
- headers- the headers to use for the response
- status- the status code to use for the response
- exchange- the current request and response
- Returns:
- a Monowith theResponseEntityfor the response
 
- 
handleUnsupportedMediaTypeStatusExceptionprotected 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 handle
- headers- the headers to use for the response
- status- the status code to use for the response
- exchange- the current request and response
- Returns:
- a Monowith theResponseEntityfor the response
 
- 
handleMissingRequestValueExceptionprotected 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 handle
- headers- the headers to use for the response
- status- the status code to use for the response
- exchange- the current request and response
- Returns:
- a Monowith theResponseEntityfor the response
 
- 
handleUnsatisfiedRequestParameterExceptionprotected 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 handle
- headers- the headers to use for the response
- status- the status code to use for the response
- exchange- the current request and response
- Returns:
- a Monowith theResponseEntityfor the response
 
- 
handleWebExchangeBindExceptionprotected 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 handle
- headers- the headers to use for the response
- status- the status code to use for the response
- exchange- the current request and response
- Returns:
- a Monowith theResponseEntityfor the response
 
- 
handleServerWebInputExceptionprotected 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 handle
- headers- the headers to use for the response
- status- the status code to use for the response
- exchange- the current request and response
- Returns:
- a Monowith theResponseEntityfor the response
 
- 
handleResponseStatusExceptionprotected 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 handle
- headers- the headers to use for the response
- status- the status code to use for the response
- exchange- the current request and response
- Returns:
- a Monowith theResponseEntityfor the response
 
- 
handleServerErrorExceptionprotected 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 handle
- headers- the headers to use for the response
- status- the status code to use for the response
- exchange- the current request and response
- Returns:
- a Monowith theResponseEntityfor the response
 
- 
handleErrorResponseExceptionprotected 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 handle
- headers- the headers to use for the response
- status- the status code to use for the response
- exchange- the current request and response
- Returns:
- a Monowith theResponseEntityfor the response
 
- 
createProblemDetailprotected 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 handled
- status- the status to associate with the exception
- defaultDetail- default value for the "detail" field
- detailMessageCode- the code to use to look up the "detail" field through a- MessageSource, falling back on- ErrorResponse.getDefaultDetailMessageCode(Class, String)
- detailMessageArguments- the arguments to go with the detailMessageCode
- Returns:
- the created ProblemDetailinstance
 
- 
handleExceptionInternalprotected 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 handle
- body- the body to use for the response
- headers- the headers to use for the response
- status- the status code to use for the response
- exchange- the current request and response
- Returns:
- a Monowith theResponseEntityfor the response
 
- return 
- 
createResponseEntityprotected 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 response
- headers- the headers to use for the response
- status- the status code to use for the response
- exchange- the current request and response
- Returns:
- a Monowith the createdResponseEntity
- Since:
- 6.0
 
 
-