Class ResponseEntityExceptionHandler
- All Implemented Interfaces:
Aware
,MessageSourceAware
@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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected ProblemDetail
createProblemDetail
(Exception ex, HttpStatusCode status, String defaultDetail, String detailMessageCode, Object[] detailMessageArguments, WebRequest request) Convenience method to create aProblemDetail
for any exception that doesn't implementErrorResponse
, also performing aMessageSource
lookup for the "detail" field.protected ResponseEntity<Object>
createResponseEntity
(Object body, HttpHeaders headers, HttpStatusCode statusCode, WebRequest request) Create theResponseEntity
to use from the given body, headers, and statusCode.protected MessageSource
Get theMessageSource
that this exception handler uses.protected ResponseEntity<Object>
handleAsyncRequestTimeoutException
(AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofAsyncRequestTimeoutException
.protected ResponseEntity<Object>
handleConversionNotSupported
(ConversionNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofConversionNotSupportedException
.protected ResponseEntity<Object>
handleErrorResponseException
(ErrorResponseException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling of anyErrorResponseException
.final ResponseEntity<Object>
handleException
(Exception ex, WebRequest request) Handle all exceptions raised within Spring MVC handling of the request.protected ResponseEntity<Object>
handleExceptionInternal
(Exception ex, Object body, HttpHeaders headers, HttpStatusCode statusCode, WebRequest request) Internal handler method that all others in this class delegate to, for common handling, and for the creation of aResponseEntity
.protected ResponseEntity<Object>
handleHandlerMethodValidationException
(HandlerMethodValidationException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHandlerMethodValidationException
.protected ResponseEntity<Object>
handleHttpMediaTypeNotAcceptable
(HttpMediaTypeNotAcceptableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpMediaTypeNotAcceptableException
.protected ResponseEntity<Object>
handleHttpMediaTypeNotSupported
(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpMediaTypeNotSupportedException
.protected ResponseEntity<Object>
handleHttpMessageNotReadable
(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpMessageNotReadableException
.protected ResponseEntity<Object>
handleHttpMessageNotWritable
(HttpMessageNotWritableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpMessageNotWritableException
.protected ResponseEntity<Object>
handleHttpRequestMethodNotSupported
(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpRequestMethodNotSupportedException
.protected ResponseEntity<Object>
handleMaxUploadSizeExceededException
(MaxUploadSizeExceededException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling of anyMaxUploadSizeExceededException
.protected ResponseEntity<Object>
handleMethodArgumentNotValid
(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofMethodArgumentNotValidException
.protected ResponseEntity<Object>
handleMethodValidationException
(MethodValidationException ex, HttpHeaders headers, HttpStatus status, WebRequest request) Customize the handling ofMethodValidationException
.protected ResponseEntity<Object>
handleMissingPathVariable
(MissingPathVariableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofMissingPathVariableException
.protected ResponseEntity<Object>
handleMissingServletRequestParameter
(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofMissingServletRequestParameterException
.protected ResponseEntity<Object>
handleMissingServletRequestPart
(MissingServletRequestPartException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofMissingServletRequestPartException
.protected ResponseEntity<Object>
handleNoHandlerFoundException
(NoHandlerFoundException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofNoHandlerFoundException
.protected ResponseEntity<Object>
handleNoResourceFoundException
(NoResourceFoundException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofNoResourceFoundException
.protected ResponseEntity<Object>
handleServletRequestBindingException
(ServletRequestBindingException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofServletRequestBindingException
.protected ResponseEntity<Object>
handleTypeMismatch
(TypeMismatchException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofTypeMismatchException
.void
setMessageSource
(MessageSource messageSource) Set theMessageSource
that this object runs in.
-
Field Details
-
PAGE_NOT_FOUND_LOG_CATEGORY
Log category to use when no mapped handler is found for a request.- See Also:
-
pageNotFoundLogger
Specific logger to use when no mapped handler is found for a request.- See Also:
-
logger
Common logger for use in subclasses.
-
-
Constructor Details
-
ResponseEntityExceptionHandler
public ResponseEntityExceptionHandler()
-
-
Method Details
-
setMessageSource
Description copied from interface:MessageSourceAware
Set theMessageSource
that 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:
setMessageSource
in interfaceMessageSourceAware
- Parameters:
messageSource
- message source to be used by this object
-
getMessageSource
Get theMessageSource
that this exception handler uses.- Since:
- 6.0.3
-
handleException
@ExceptionHandler({HttpRequestMethodNotSupportedException.class,HttpMediaTypeNotSupportedException.class,HttpMediaTypeNotAcceptableException.class,MissingPathVariableException.class,MissingServletRequestParameterException.class,MissingServletRequestPartException.class,ServletRequestBindingException.class,MethodArgumentNotValidException.class,HandlerMethodValidationException.class,NoHandlerFoundException.class,NoResourceFoundException.class,AsyncRequestTimeoutException.class,ErrorResponseException.class,MaxUploadSizeExceededException.class,ConversionNotSupportedException.class,TypeMismatchException.class,HttpMessageNotReadableException.class,HttpMessageNotWritableException.class,MethodValidationException.class,BindException.class}) @Nullable public final ResponseEntity<Object> handleException(Exception ex, WebRequest request) throws Exception Handle all exceptions raised within Spring MVC handling of the request.- Parameters:
ex
- the exception to handlerequest
- the current request- Throws:
Exception
-
handleHttpRequestMethodNotSupported
@Nullable protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpRequestMethodNotSupportedException
.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 handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed
-
handleHttpMediaTypeNotSupported
@Nullable protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpMediaTypeNotSupportedException
.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 handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed
-
handleHttpMediaTypeNotAcceptable
@Nullable protected ResponseEntity<Object> handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpMediaTypeNotAcceptableException
.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 handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed
-
handleMissingPathVariable
@Nullable protected ResponseEntity<Object> handleMissingPathVariable(MissingPathVariableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofMissingPathVariableException
.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 handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed - Since:
- 4.2
-
handleMissingServletRequestParameter
@Nullable protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofMissingServletRequestParameterException
.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 handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed
-
handleMissingServletRequestPart
@Nullable protected ResponseEntity<Object> handleMissingServletRequestPart(MissingServletRequestPartException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofMissingServletRequestPartException
.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 handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed
-
handleServletRequestBindingException
@Nullable protected ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofServletRequestBindingException
.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 handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed
-
handleMethodArgumentNotValid
@Nullable protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofMethodArgumentNotValidException
.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 handleheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed
-
handleHandlerMethodValidationException
@Nullable protected ResponseEntity<Object> handleHandlerMethodValidationException(HandlerMethodValidationException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHandlerMethodValidationException
.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 handleheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed - Since:
- 6.1
-
handleNoHandlerFoundException
@Nullable protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofNoHandlerFoundException
.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 handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed - Since:
- 4.0
-
handleNoResourceFoundException
@Nullable protected ResponseEntity<Object> handleNoResourceFoundException(NoResourceFoundException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofNoResourceFoundException
.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 handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed - Since:
- 6.1
-
handleAsyncRequestTimeoutException
@Nullable protected ResponseEntity<Object> handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofAsyncRequestTimeoutException
.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 handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed - Since:
- 4.2.8
-
handleErrorResponseException
@Nullable protected ResponseEntity<Object> handleErrorResponseException(ErrorResponseException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) 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.context.request.WebRequest)
.- Parameters:
ex
- the exception to handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed - Since:
- 6.0
-
handleMaxUploadSizeExceededException
@Nullable protected ResponseEntity<Object> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling of anyMaxUploadSizeExceededException
.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 handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed - Since:
- 6.1
-
handleConversionNotSupported
@Nullable protected ResponseEntity<Object> handleConversionNotSupported(ConversionNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofConversionNotSupportedException
.By default this method creates a
ProblemDetail
with the status and a short detail message, and also looks up an override for the detail viaMessageSource
, before delegating tohandleExceptionInternal(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 handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed
-
handleTypeMismatch
@Nullable protected ResponseEntity<Object> handleTypeMismatch(TypeMismatchException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofTypeMismatchException
.By default this method creates a
ProblemDetail
with the status and a short detail message, and also looks up an override for the detail viaMessageSource
, before delegating tohandleExceptionInternal(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 handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed
-
handleHttpMessageNotReadable
@Nullable protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpMessageNotReadableException
.By default this method creates a
ProblemDetail
with the status and a short detail message, and also looks up an override for the detail viaMessageSource
, before delegating tohandleExceptionInternal(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 handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed
-
handleHttpMessageNotWritable
@Nullable protected ResponseEntity<Object> handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpMessageNotWritableException
.By default this method creates a
ProblemDetail
with the status and a short detail message, and also looks up an override for the detail viaMessageSource
, before delegating tohandleExceptionInternal(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 handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed
-
handleMethodValidationException
@Nullable protected ResponseEntity<Object> handleMethodValidationException(MethodValidationException ex, HttpHeaders headers, HttpStatus status, WebRequest request) Customize the handling ofMethodValidationException
.By default this method creates a
ProblemDetail
with the status and a short detail message, and also looks up an override for the detail viaMessageSource
, before delegating tohandleExceptionInternal(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 handleheaders
- the headers to use for the responsestatus
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed - Since:
- 6.1
-
createProblemDetail
protected ProblemDetail createProblemDetail(Exception ex, HttpStatusCode status, String defaultDetail, @Nullable String detailMessageCode, @Nullable Object[] detailMessageArguments, WebRequest request) Convenience method to create aProblemDetail
for any exception that doesn't implementErrorResponse
, also performing aMessageSource
lookup for the "detail" field.- Parameters:
ex
- the exception being handledstatus
- the status to associate with the exceptiondefaultDetail
- default value for the "detail" fielddetailMessageCode
- the code to use to look up the "detail" field through aMessageSource
; ifnull
thenErrorResponse.getDefaultDetailMessageCode(Class, String)
is used to determine the default message code to usedetailMessageArguments
- the arguments to go with the detailMessageCoderequest
- the current request- Returns:
- the created
ProblemDetail
instance - Since:
- 6.0
-
handleExceptionInternal
@Nullable protected ResponseEntity<Object> handleExceptionInternal(Exception ex, @Nullable Object body, HttpHeaders headers, HttpStatusCode statusCode, WebRequest request) 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 responsestatusCode
- the status code to use for the responserequest
- the current request- Returns:
- a
ResponseEntity
for the response to use, possiblynull
when the response is already committed
- return
-
createResponseEntity
protected ResponseEntity<Object> createResponseEntity(@Nullable Object body, HttpHeaders headers, HttpStatusCode statusCode, WebRequest request) 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 responsestatusCode
- the status code to use for the responserequest
- the current request- Returns:
- the
ResponseEntity
instance to use - Since:
- 6.0
-