org.springframework.web.servlet.mvc.method.annotation

## Class ResponseEntityExceptionHandler

- java.lang.Object
- - org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler

-     public abstract class ResponseEntityExceptionHandler
      extends Object

  A convenient base class for `@ControllerAdvice` classes that wish to provide centralized exception handling across all `@RequestMapping` methods through `@ExceptionHandler` methods.

  This base class provides an `@ExceptionHandler` for handling standard Spring MVC exceptions that returns a `ResponseEntity` to be written with `message converters`. This is in contrast to `DefaultHandlerExceptionResolver` which returns a `ModelAndView` instead.

  If there is no need to write error content to the response body, or if using view resolution, e.g. `ContentNegotiatingViewResolver`, then use `DefaultHandlerExceptionResolver` instead.

  Note that in order for an `@ControllerAdvice` sub-class to be detected, `ExceptionHandlerExceptionResolver` must be configured.

  Since:
    3.2
  Author:
    Rossen Stoyanchev
  See Also:
    `DefaultHandlerExceptionResolver`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected Log` | `logger`
    `static String` | `PAGE_NOT_FOUND_LOG_CATEGORY` Log category to use when no mapped handler is found for a request.
    `protected static Log` | `pageNotFoundLogger` Additional logger to use when no mapped handler is found for a request.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ResponseEntityExceptionHandler()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected ResponseEntity<Object>` | `handleBindException(BindException ex, HttpHeaders headers, HttpStatus status, WebRequest request)` Customize the response for BindException.
    `protected ResponseEntity<Object>` | `handleConversionNotSupported(ConversionNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request)` Customize the response for ConversionNotSupportedException.
    `ResponseEntity<Object>` | `handleException(Exception ex, WebRequest request)` Provides handling for standard Spring MVC exceptions.
    `protected ResponseEntity<Object>` | `handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request)` A single place to customize the response body of all Exception types.
    `protected ResponseEntity<Object>` | `handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex, HttpHeaders headers, HttpStatus status, WebRequest request)` Customize the response for HttpMediaTypeNotAcceptableException.
    `protected ResponseEntity<Object>` | `handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request)` Customize the response for HttpMediaTypeNotSupportedException.
    `protected ResponseEntity<Object>` | `handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request)` Customize the response for HttpMessageNotReadableException.
    `protected ResponseEntity<Object>` | `handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpHeaders headers, HttpStatus status, WebRequest request)` Customize the response for HttpMessageNotWritableException.
    `protected ResponseEntity<Object>` | `handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request)` Customize the response for HttpRequestMethodNotSupportedException.
    `protected ResponseEntity<Object>` | `handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request)` Customize the response for MethodArgumentNotValidException.
    `protected ResponseEntity<Object>` | `handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, WebRequest request)` Customize the response for MissingServletRequestParameterException.
    `protected ResponseEntity<Object>` | `handleMissingServletRequestPart(MissingServletRequestPartException ex, HttpHeaders headers, HttpStatus status, WebRequest request)` Customize the response for MissingServletRequestPartException.
    `protected ResponseEntity<Object>` | `handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest request)` Customize the response for NoHandlerFoundException.
    `protected ResponseEntity<Object>` | `handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex, HttpHeaders headers, HttpStatus status, WebRequest request)` Customize the response for NoSuchRequestHandlingMethodException.
    `protected ResponseEntity<Object>` | `handleServletRequestBindingException(ServletRequestBindingException ex, HttpHeaders headers, HttpStatus status, WebRequest request)` Customize the response for ServletRequestBindingException.
    `protected ResponseEntity<Object>` | `handleTypeMismatch(TypeMismatchException ex, HttpHeaders headers, HttpStatus status, WebRequest request)` Customize the response for TypeMismatchException.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### logger

          protected final Log logger

    - #### PAGE_NOT_FOUND_LOG_CATEGORY

          public static final String PAGE_NOT_FOUND_LOG_CATEGORY

      Log category to use when no mapped handler is found for a request.
      See Also:
        `pageNotFoundLogger`, Constant Field Values

    - #### pageNotFoundLogger

          protected static final Log pageNotFoundLogger

      Additional logger to use when no mapped handler is found for a request.
      See Also:
        `PAGE_NOT_FOUND_LOG_CATEGORY`

  - ### Constructor Detail

    - #### ResponseEntityExceptionHandler

          public ResponseEntityExceptionHandler()

  - ### Method Detail

    - #### handleException

          @ExceptionHandler(value={NoSuchRequestHandlingMethodException.class,HttpRequestMethodNotSupportedException.class,HttpMediaTypeNotSupportedException.class,HttpMediaTypeNotAcceptableException.class,MissingServletRequestParameterException.class,ServletRequestBindingException.class,ConversionNotSupportedException.class,TypeMismatchException.class,HttpMessageNotReadableException.class,HttpMessageNotWritableException.class,MethodArgumentNotValidException.class,MissingServletRequestPartException.class,BindException.class,NoHandlerFoundException.class})
          public final ResponseEntity<Object> handleException(Exception ex,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      WebRequest request)

      Provides handling for standard Spring MVC exceptions.
      Parameters:
        `ex` - the target exception
        `request` - the current request

    - #### handleExceptionInternal

          protected ResponseEntity<Object> handleExceptionInternal(Exception ex,
                                                                   Object body,
                                                                   HttpHeaders headers,
                                                                   HttpStatus status,
                                                                   WebRequest request)

      A single place to customize the response body of all Exception types. This method returns `null` by default.
      Parameters:
        `ex` - the exception
        `body` - the body to use for the response
        `headers` - the headers to be written to the response
        `status` - the selected response status
        `request` - the current request

    - #### handleNoSuchRequestHandlingMethod

          protected ResponseEntity<Object> handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex,
                                                                             HttpHeaders headers,
                                                                             HttpStatus status,
                                                                             WebRequest request)

      Customize the response for NoSuchRequestHandlingMethodException. This method logs a warning and delegates to `handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)`.
      Parameters:
        `ex` - the exception
        `headers` - the headers to be written to the response
        `status` - the selected response status
        `request` - the current request
      Returns:
        a `ResponseEntity` instance

    - #### handleHttpRequestMethodNotSupported

          protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
                                                                               HttpHeaders headers,
                                                                               HttpStatus status,
                                                                               WebRequest request)

      Customize the response for HttpRequestMethodNotSupportedException. This method logs a warning, sets the "Allow" header, and delegates to `handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)`.
      Parameters:
        `ex` - the exception
        `headers` - the headers to be written to the response
        `status` - the selected response status
        `request` - the current request
      Returns:
        a `ResponseEntity` instance

    - #### handleHttpMediaTypeNotSupported

          protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,
                                                                           HttpHeaders headers,
                                                                           HttpStatus status,
                                                                           WebRequest request)

      Customize the response for HttpMediaTypeNotSupportedException. This method sets the "Accept" header and delegates to `handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)`.
      Parameters:
        `ex` - the exception
        `headers` - the headers to be written to the response
        `status` - the selected response status
        `request` - the current request
      Returns:
        a `ResponseEntity` instance

    - #### handleHttpMediaTypeNotAcceptable

          protected ResponseEntity<Object> handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex,
                                                                            HttpHeaders headers,
                                                                            HttpStatus status,
                                                                            WebRequest request)

      Customize the response for HttpMediaTypeNotAcceptableException. This method delegates to `handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)`.
      Parameters:
        `ex` - the exception
        `headers` - the headers to be written to the response
        `status` - the selected response status
        `request` - the current request
      Returns:
        a `ResponseEntity` instance

    - #### handleMissingServletRequestParameter

          protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex,
                                                                                HttpHeaders headers,
                                                                                HttpStatus status,
                                                                                WebRequest request)

      Customize the response for MissingServletRequestParameterException. This method delegates to `handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)`.
      Parameters:
        `ex` - the exception
        `headers` - the headers to be written to the response
        `status` - the selected response status
        `request` - the current request
      Returns:
        a `ResponseEntity` instance

    - #### handleServletRequestBindingException

          protected ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex,
                                                                                HttpHeaders headers,
                                                                                HttpStatus status,
                                                                                WebRequest request)

      Customize the response for ServletRequestBindingException. This method delegates to `handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)`.
      Parameters:
        `ex` - the exception
        `headers` - the headers to be written to the response
        `status` - the selected response status
        `request` - the current request
      Returns:
        a `ResponseEntity` instance

    - #### handleConversionNotSupported

          protected ResponseEntity<Object> handleConversionNotSupported(ConversionNotSupportedException ex,
                                                                        HttpHeaders headers,
                                                                        HttpStatus status,
                                                                        WebRequest request)

      Customize the response for ConversionNotSupportedException. This method delegates to `handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)`.
      Parameters:
        `ex` - the exception
        `headers` - the headers to be written to the response
        `status` - the selected response status
        `request` - the current request
      Returns:
        a `ResponseEntity` instance

    - #### handleTypeMismatch

          protected ResponseEntity<Object> handleTypeMismatch(TypeMismatchException ex,
                                                              HttpHeaders headers,
                                                              HttpStatus status,
                                                              WebRequest request)

      Customize the response for TypeMismatchException. This method delegates to `handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)`.
      Parameters:
        `ex` - the exception
        `headers` - the headers to be written to the response
        `status` - the selected response status
        `request` - the current request
      Returns:
        a `ResponseEntity` instance

    - #### handleHttpMessageNotReadable

          protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
                                                                        HttpHeaders headers,
                                                                        HttpStatus status,
                                                                        WebRequest request)

      Customize the response for HttpMessageNotReadableException. This method delegates to `handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)`.
      Parameters:
        `ex` - the exception
        `headers` - the headers to be written to the response
        `status` - the selected response status
        `request` - the current request
      Returns:
        a `ResponseEntity` instance

    - #### handleHttpMessageNotWritable

          protected ResponseEntity<Object> handleHttpMessageNotWritable(HttpMessageNotWritableException ex,
                                                                        HttpHeaders headers,
                                                                        HttpStatus status,
                                                                        WebRequest request)

      Customize the response for HttpMessageNotWritableException. This method delegates to `handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)`.
      Parameters:
        `ex` - the exception
        `headers` - the headers to be written to the response
        `status` - the selected response status
        `request` - the current request
      Returns:
        a `ResponseEntity` instance

    - #### handleMethodArgumentNotValid

          protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
                                                                        HttpHeaders headers,
                                                                        HttpStatus status,
                                                                        WebRequest request)

      Customize the response for MethodArgumentNotValidException. This method delegates to `handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)`.
      Parameters:
        `ex` - the exception
        `headers` - the headers to be written to the response
        `status` - the selected response status
        `request` - the current request
      Returns:
        a `ResponseEntity` instance

    - #### handleMissingServletRequestPart

          protected ResponseEntity<Object> handleMissingServletRequestPart(MissingServletRequestPartException ex,
                                                                           HttpHeaders headers,
                                                                           HttpStatus status,
                                                                           WebRequest request)

      Customize the response for MissingServletRequestPartException. This method delegates to `handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)`.
      Parameters:
        `ex` - the exception
        `headers` - the headers to be written to the response
        `status` - the selected response status
        `request` - the current request
      Returns:
        a `ResponseEntity` instance

    - #### handleBindException

          protected ResponseEntity<Object> handleBindException(BindException ex,
                                                               HttpHeaders headers,
                                                               HttpStatus status,
                                                               WebRequest request)

      Customize the response for BindException. This method delegates to `handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)`.
      Parameters:
        `ex` - the exception
        `headers` - the headers to be written to the response
        `status` - the selected response status
        `request` - the current request
      Returns:
        a `ResponseEntity` instance

    - #### handleNoHandlerFoundException

          protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex,
                                                                         HttpHeaders headers,
                                                                         HttpStatus status,
                                                                         WebRequest request)

      Customize the response for NoHandlerFoundException. This method delegates to `handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)`.
      Parameters:
        `ex` - the exception
        `headers` - the headers to be written to the response
        `status` - the selected response status
        `request` - the current request
      Returns:
        a `ResponseEntity` instance
      Since:
        4.0