Class ResponseEntityExceptionHandler

java.lang.Object
org.springframework.web.reactive.result.method.annotation.ResponseEntityExceptionHandler
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.context.MessageSourceAware

public abstract class ResponseEntityExceptionHandler extends Object implements org.springframework.context.MessageSourceAware
A class with an @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
    Modifier and Type
    Field
    Description
    protected final Log
    Common logger for use in subclasses.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected org.springframework.http.ProblemDetail
    createProblemDetail(Exception ex, org.springframework.http.HttpStatusCode status, String defaultDetail, String detailMessageCode, Object[] detailMessageArguments, org.springframework.web.server.ServerWebExchange exchange)
    Convenience method to create a ProblemDetail for any exception that doesn't implement ErrorResponse, also performing a MessageSource lookup for the "detail" field.
    protected reactor.core.publisher.Mono<org.springframework.http.ResponseEntity<Object>>
    createResponseEntity(Object body, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.server.ServerWebExchange exchange)
    Create the ResponseEntity to use from the given body, headers, and statusCode.
    protected org.springframework.context.MessageSource
    Get the MessageSource that this exception handler uses.
    protected reactor.core.publisher.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 any ErrorResponseException.
    final reactor.core.publisher.Mono<org.springframework.http.ResponseEntity<Object>>
    handleException(Exception ex, org.springframework.web.server.ServerWebExchange exchange)
    Handle all exceptions raised within Spring MVC handling of the request.
    protected reactor.core.publisher.Mono<org.springframework.http.ResponseEntity<Object>>
    handleExceptionInternal(Exception ex, Object body, 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 a ResponseEntity.
    protected reactor.core.publisher.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 of MethodNotAllowedException.
    protected reactor.core.publisher.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 of MissingRequestValueException.
    protected reactor.core.publisher.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 of NotAcceptableStatusException.
    protected reactor.core.publisher.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 any ResponseStatusException.
    protected reactor.core.publisher.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 of ServerErrorException.
    protected reactor.core.publisher.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 of ServerWebInputException.
    protected reactor.core.publisher.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 of UnsatisfiedRequestParameterException.
    protected reactor.core.publisher.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 of UnsupportedMediaTypeStatusException.
    protected reactor.core.publisher.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 of WebExchangeBindException.
    void
    setMessageSource(org.springframework.context.MessageSource messageSource)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • logger

      protected final Log logger
      Common logger for use in subclasses.
  • Constructor Details

    • ResponseEntityExceptionHandler

      public ResponseEntityExceptionHandler()
  • Method Details

    • setMessageSource

      public void setMessageSource(org.springframework.context.MessageSource messageSource)
      Specified by:
      setMessageSource in interface org.springframework.context.MessageSourceAware
    • getMessageSource

      @Nullable protected org.springframework.context.MessageSource getMessageSource()
      Get the MessageSource that 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.server.ServerWebInputException.class,org.springframework.web.server.ServerErrorException.class,org.springframework.web.server.ResponseStatusException.class,org.springframework.web.ErrorResponseException.class}) public final reactor.core.publisher.Mono<org.springframework.http.ResponseEntity<Object>> handleException(Exception ex, org.springframework.web.server.ServerWebExchange exchange)
      Handle all exceptions raised within Spring MVC handling of the request.
      Parameters:
      ex - the exception to handle
      exchange - the current request-response
    • handleMethodNotAllowedException

      protected reactor.core.publisher.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)
      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 Mono with the ResponseEntity for the response
    • handleNotAcceptableStatusException

      protected reactor.core.publisher.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)
      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 Mono with the ResponseEntity for the response
    • handleUnsupportedMediaTypeStatusException

      protected reactor.core.publisher.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)
      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 Mono with the ResponseEntity for the response
    • handleMissingRequestValueException

      protected reactor.core.publisher.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)
      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 Mono with the ResponseEntity for the response
    • handleUnsatisfiedRequestParameterException

      protected reactor.core.publisher.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)
      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 Mono with the ResponseEntity for the response
    • handleWebExchangeBindException

      protected reactor.core.publisher.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)
      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 Mono with the ResponseEntity for the response
    • handleServerWebInputException

      protected reactor.core.publisher.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)
      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 Mono with the ResponseEntity for the response
    • handleResponseStatusException

      protected reactor.core.publisher.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)
      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 Mono with the ResponseEntity for the response
    • handleServerErrorException

      protected reactor.core.publisher.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)
      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 Mono with the ResponseEntity for the response
    • handleErrorResponseException

      protected reactor.core.publisher.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)
      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 Mono with the ResponseEntity for the response
    • createProblemDetail

      protected org.springframework.http.ProblemDetail createProblemDetail(Exception ex, org.springframework.http.HttpStatusCode status, String defaultDetail, @Nullable String detailMessageCode, @Nullable Object[] detailMessageArguments, org.springframework.web.server.ServerWebExchange exchange)
      Convenience method to create a ProblemDetail for any exception that doesn't implement ErrorResponse, also performing a MessageSource lookup 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 ProblemDetail instance
    • handleExceptionInternal

      protected reactor.core.publisher.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 a ResponseEntity.

      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 from ErrorResponse exceptions, if the body is null.
      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 Mono with the ResponseEntity for the response
    • createResponseEntity

      protected reactor.core.publisher.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 the ResponseEntity 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 of ProblemDetail as an extension of ProblemDetail.
      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 Mono with the created ResponseEntity
      Since:
      6.0