Class ResponseEntityExceptionHandler
@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 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 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 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
.
-
Field Details
-
logger
Common logger for use in subclasses.
-
-
Constructor Details
-
ResponseEntityExceptionHandler
public ResponseEntityExceptionHandler()
-
-
Method Details
-
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
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
-
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
-
handleExceptionInternal
protected reactor.core.publisher.Mono<ResponseEntity<Object>> handleExceptionInternal(Exception ex, @Nullable 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
.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, 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
-