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

# Class ResponseEntityExceptionHandler

java.lang.Object
org.springframework.web.servlet.mvc.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 MVC raised exceptions by returning a `ResponseEntity` with RFC 9457 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.context.request.WebRequest)` to override common handling of all exceptions, or override `createResponseEntity(java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)` to intercept the final step of creating the `ResponseEntity` from the selected HTTP status code, headers, and body.

Since:
  3.2
Author:
  Rossen Stoyanchev

## Field Summary

Fields
Modifier and Type
Field
Description
`protected final org.apache.commons.logging.Log`
`logger`
Common logger for use in subclasses.
`static final String`
`PAGE_NOT_FOUND_LOG_CATEGORY`
Log category to use when no mapped handler is found for a request.
`protected static final org.apache.commons.logging.Log`
`pageNotFoundLogger`
Specific logger to use when no mapped handler is found for a request.

## Constructor Summary

Constructors
Constructor
Description
`ResponseEntityExceptionHandler()`

## 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.context.request.WebRequest request)`
Convenience method to create a `ProblemDetail` for any exception that doesn't implement `ErrorResponse`, also performing a `MessageSource` lookup for the "detail" field.
`protected org.springframework.http.ResponseEntity<Object>`
`createResponseEntity(Object body, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode statusCode, org.springframework.web.context.request.WebRequest request)`
Create the `ResponseEntity` to use from the given body, headers, and statusCode.
`protected org.springframework.context.MessageSource`
`getMessageSource()`
Get the `MessageSource` that this exception handler uses.
`protected org.springframework.http.ResponseEntity<Object>`
`handleAsyncRequestNotUsableException(org.springframework.web.context.request.async.AsyncRequestNotUsableException ex, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `AsyncRequestNotUsableException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleAsyncRequestTimeoutException(org.springframework.web.context.request.async.AsyncRequestTimeoutException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `AsyncRequestTimeoutException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleConversionNotSupported(org.springframework.beans.ConversionNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `ConversionNotSupportedException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleErrorResponseException(org.springframework.web.ErrorResponseException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of any `ErrorResponseException`.
`final org.springframework.http.ResponseEntity<Object>`
`handleException(Exception ex, org.springframework.web.context.request.WebRequest request)`
Handle all exceptions raised within Spring MVC handling of the request.
`protected org.springframework.http.ResponseEntity<Object>`
`handleExceptionInternal(Exception ex, Object body, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode statusCode, org.springframework.web.context.request.WebRequest request)`
Internal handler method that all others in this class delegate to, for common handling, and for the creation of a `ResponseEntity`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleHandlerMethodValidationException(org.springframework.web.method.annotation.HandlerMethodValidationException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `HandlerMethodValidationException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleHttpMediaTypeNotAcceptable(org.springframework.web.HttpMediaTypeNotAcceptableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `HttpMediaTypeNotAcceptableException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleHttpMediaTypeNotSupported(org.springframework.web.HttpMediaTypeNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `HttpMediaTypeNotSupportedException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleHttpMessageNotReadable(org.springframework.http.converter.HttpMessageNotReadableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `HttpMessageNotReadableException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleHttpMessageNotWritable(org.springframework.http.converter.HttpMessageNotWritableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `HttpMessageNotWritableException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleHttpRequestMethodNotSupported(org.springframework.web.HttpRequestMethodNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `HttpRequestMethodNotSupportedException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleMaxUploadSizeExceededException(org.springframework.web.multipart.MaxUploadSizeExceededException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of any `MaxUploadSizeExceededException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleMethodArgumentNotValid(org.springframework.web.bind.MethodArgumentNotValidException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `MethodArgumentNotValidException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleMethodValidationException(org.springframework.validation.method.MethodValidationException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `MethodValidationException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleMissingPathVariable(org.springframework.web.bind.MissingPathVariableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `MissingPathVariableException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleMissingServletRequestParameter(org.springframework.web.bind.MissingServletRequestParameterException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `MissingServletRequestParameterException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleMissingServletRequestPart(org.springframework.web.multipart.support.MissingServletRequestPartException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `MissingServletRequestPartException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleNoHandlerFoundException(NoHandlerFoundException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `NoHandlerFoundException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleNoResourceFoundException(NoResourceFoundException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `NoResourceFoundException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `ServletRequestBindingException`.
`protected org.springframework.http.ResponseEntity<Object>`
`handleTypeMismatch(org.springframework.beans.TypeMismatchException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)`
Customize the handling of `TypeMismatchException`.
`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

### 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 org.apache.commons.logging.Log pageNotFoundLogger
Specific logger to use when no mapped handler is found for a request.

See Also:
  - `PAGE_NOT_FOUND_LOG_CATEGORY`

### logger

protected final org.apache.commons.logging.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.HttpRequestMethodNotSupportedException.class,org.springframework.web.HttpMediaTypeNotSupportedException.class,org.springframework.web.HttpMediaTypeNotAcceptableException.class,org.springframework.web.bind.MissingPathVariableException.class,org.springframework.web.bind.MissingServletRequestParameterException.class,org.springframework.web.multipart.support.MissingServletRequestPartException.class,org.springframework.web.bind.ServletRequestBindingException.class,org.springframework.web.bind.MethodArgumentNotValidException.class,org.springframework.web.method.annotation.HandlerMethodValidationException.class,NoHandlerFoundException.class,NoResourceFoundException.class,org.springframework.web.context.request.async.AsyncRequestTimeoutException.class,org.springframework.web.ErrorResponseException.class,org.springframework.web.multipart.MaxUploadSizeExceededException.class,org.springframework.beans.ConversionNotSupportedException.class,org.springframework.beans.TypeMismatchException.class,org.springframework.http.converter.HttpMessageNotReadableException.class,org.springframework.http.converter.HttpMessageNotWritableException.class,org.springframework.validation.method.MethodValidationException.class,org.springframework.web.context.request.async.AsyncRequestNotUsableException.class}) @Nullable public final org.springframework.http.ResponseEntity<Object> handleException(Exception ex, org.springframework.web.context.request.WebRequest request) throws Exception
Handle all exceptions raised within Spring MVC handling of the request.

Parameters:
  `ex` - the exception to handle
  `request` - the current request
Throws:
  `Exception`

### handleHttpRequestMethodNotSupported

@Nullable protected org.springframework.http.ResponseEntity<Object> handleHttpRequestMethodNotSupported(org.springframework.web.HttpRequestMethodNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `HttpRequestMethodNotSupportedException`.

This method logs a warning and delegates to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed

### handleHttpMediaTypeNotSupported

@Nullable protected org.springframework.http.ResponseEntity<Object> handleHttpMediaTypeNotSupported(org.springframework.web.HttpMediaTypeNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `HttpMediaTypeNotSupportedException`.

This method delegates to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed

### handleHttpMediaTypeNotAcceptable

@Nullable protected org.springframework.http.ResponseEntity<Object> handleHttpMediaTypeNotAcceptable(org.springframework.web.HttpMediaTypeNotAcceptableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `HttpMediaTypeNotAcceptableException`.

This method delegates to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed

### handleMissingPathVariable

@Nullable protected org.springframework.http.ResponseEntity<Object> handleMissingPathVariable(org.springframework.web.bind.MissingPathVariableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `MissingPathVariableException`.

This method delegates to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed
Since:
  4.2

### handleMissingServletRequestParameter

@Nullable protected org.springframework.http.ResponseEntity<Object> handleMissingServletRequestParameter(org.springframework.web.bind.MissingServletRequestParameterException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `MissingServletRequestParameterException`.

This method delegates to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed

### handleMissingServletRequestPart

@Nullable protected org.springframework.http.ResponseEntity<Object> handleMissingServletRequestPart(org.springframework.web.multipart.support.MissingServletRequestPartException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `MissingServletRequestPartException`.

This method delegates to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed

### handleServletRequestBindingException

@Nullable protected org.springframework.http.ResponseEntity<Object> handleServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `ServletRequestBindingException`.

This method delegates to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed

### handleMethodArgumentNotValid

@Nullable protected org.springframework.http.ResponseEntity<Object> handleMethodArgumentNotValid(org.springframework.web.bind.MethodArgumentNotValidException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `MethodArgumentNotValidException`.

This method delegates to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to be written to the response
  `status` - the selected response status
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed

### handleHandlerMethodValidationException

@Nullable protected org.springframework.http.ResponseEntity<Object> handleHandlerMethodValidationException(org.springframework.web.method.annotation.HandlerMethodValidationException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `HandlerMethodValidationException`.

This method delegates to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to be written to the response
  `status` - the selected response status
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed
Since:
  6.1

### handleNoHandlerFoundException

@Nullable protected org.springframework.http.ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `NoHandlerFoundException`.

This method delegates to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed
Since:
  4.0

### handleNoResourceFoundException

@Nullable protected org.springframework.http.ResponseEntity<Object> handleNoResourceFoundException(NoResourceFoundException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `NoResourceFoundException`.

This method delegates to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed
Since:
  6.1

### handleAsyncRequestTimeoutException

@Nullable protected org.springframework.http.ResponseEntity<Object> handleAsyncRequestTimeoutException(org.springframework.web.context.request.async.AsyncRequestTimeoutException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `AsyncRequestTimeoutException`.

This method delegates to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed
Since:
  4.2.8

### handleErrorResponseException

@Nullable protected org.springframework.http.ResponseEntity<Object> handleErrorResponseException(org.springframework.web.ErrorResponseException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of any `ErrorResponseException`.

This method delegates to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed
Since:
  6.0

### handleMaxUploadSizeExceededException

@Nullable protected org.springframework.http.ResponseEntity<Object> handleMaxUploadSizeExceededException(org.springframework.web.multipart.MaxUploadSizeExceededException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of any `MaxUploadSizeExceededException`.

This method delegates to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed
Since:
  6.1

### handleConversionNotSupported

@Nullable protected org.springframework.http.ResponseEntity<Object> handleConversionNotSupported(org.springframework.beans.ConversionNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `ConversionNotSupportedException`.

By default this method creates a `ProblemDetail` with the status and a short detail message, and also looks up an override for the detail via `MessageSource`, before delegating to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed

### handleTypeMismatch

@Nullable protected org.springframework.http.ResponseEntity<Object> handleTypeMismatch(org.springframework.beans.TypeMismatchException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `TypeMismatchException`.

By default, this method creates a `ProblemDetail` with the status and a short detail message, and also looks up an override for the detail via `MessageSource`, before delegating to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed

### handleHttpMessageNotReadable

@Nullable protected org.springframework.http.ResponseEntity<Object> handleHttpMessageNotReadable(org.springframework.http.converter.HttpMessageNotReadableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `HttpMessageNotReadableException`.

By default this method creates a `ProblemDetail` with the status and a short detail message, and also looks up an override for the detail via `MessageSource`, before delegating to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed

### handleHttpMessageNotWritable

@Nullable protected org.springframework.http.ResponseEntity<Object> handleHttpMessageNotWritable(org.springframework.http.converter.HttpMessageNotWritableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `HttpMessageNotWritableException`.

By default this method creates a `ProblemDetail` with the status and a short detail message, and also looks up an override for the detail via `MessageSource`, before delegating to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed

### handleMethodValidationException

@Nullable protected org.springframework.http.ResponseEntity<Object> handleMethodValidationException(org.springframework.validation.method.MethodValidationException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request)
Customize the handling of `MethodValidationException`.

By default this method creates a `ProblemDetail` with the status and a short detail message, and also looks up an override for the detail via `MessageSource`, before delegating to `handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatusCode, org.springframework.web.context.request.WebRequest)`.

Parameters:
  `ex` - the exception to handle
  `headers` - the headers to use for the response
  `status` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed
Since:
  6.1

### handleAsyncRequestNotUsableException

@Nullable protected org.springframework.http.ResponseEntity<Object> handleAsyncRequestNotUsableException(org.springframework.web.context.request.async.AsyncRequestNotUsableException ex, org.springframework.web.context.request.WebRequest request)
Customize the handling of `AsyncRequestNotUsableException`.

By default, return `null` since the response is not usable.

Parameters:
  `ex` - the exception to handle
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed
Since:
  6.2

### 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.context.request.WebRequest request)
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`; if `null` then `ErrorResponse.getDefaultDetailMessageCode(Class, String)` is used to determine the default message code to use
  `detailMessageArguments` - the arguments to go with the detailMessageCode
  `request` - the current request
Returns:
  the created `ProblemDetail` instance
Since:
  6.0

### handleExceptionInternal

@Nullable protected org.springframework.http.ResponseEntity<Object> handleExceptionInternal(Exception ex, @Nullable Object body, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode statusCode, org.springframework.web.context.request.WebRequest request)
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
  `statusCode` - the status code to use for the response
  `request` - the current request
Returns:
  a `ResponseEntity` for the response to use, possibly `null` when the response is already committed

### createResponseEntity

protected org.springframework.http.ResponseEntity<Object> createResponseEntity(@Nullable Object body, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode statusCode, org.springframework.web.context.request.WebRequest request)
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, for example, 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
  `statusCode` - the status code to use for the response
  `request` - the current request
Returns:
  the `ResponseEntity` instance to use
Since:
  6.0