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(Exception, Object, HttpHeaders, HttpStatusCode, WebRequest) to override common handling of all exceptions,
or override createResponseEntity(Object, HttpHeaders, HttpStatusCode, 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 SummaryFieldsModifier and TypeFieldDescriptionprotected final org.apache.commons.logging.LogCommon logger for use in subclasses.static final StringLog category to use when no mapped handler is found for a request.protected static final org.apache.commons.logging.LogSpecific logger to use when no mapped handler is found for a request.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionprotected ProblemDetailcreateProblemDetail(Exception ex, HttpStatusCode status, String defaultDetail, @Nullable String detailMessageCode, Object @Nullable [] detailMessageArguments, WebRequest request) Convenience method to create aProblemDetailfor any exception that doesn't implementErrorResponse, also performing aMessageSourcelookup for the "detail" field.protected ResponseEntity<Object> createResponseEntity(@Nullable Object body, HttpHeaders headers, HttpStatusCode statusCode, WebRequest request) Create theResponseEntityto use from the given body, headers, and statusCode.protected @Nullable MessageSourceGet theMessageSourcethat this exception handler uses.protected @Nullable ResponseEntity<Object> Customize the handling ofAsyncRequestNotUsableException.protected @Nullable ResponseEntity<Object> handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofAsyncRequestTimeoutException.protected @Nullable ResponseEntity<Object> handleConversionNotSupported(ConversionNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofConversionNotSupportedException.protected @Nullable ResponseEntity<Object> handleErrorResponseException(ErrorResponseException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling of anyErrorResponseException.final @Nullable ResponseEntity<Object> handleException(Exception ex, WebRequest request) Handle all exceptions raised within Spring MVC handling of the request.protected @Nullable 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.protected @Nullable ResponseEntity<Object> handleHandlerMethodValidationException(HandlerMethodValidationException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHandlerMethodValidationException.protected @Nullable ResponseEntity<Object> handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpMediaTypeNotAcceptableException.protected @Nullable ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpMediaTypeNotSupportedException.protected @Nullable ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpMessageNotReadableException.protected @Nullable ResponseEntity<Object> handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpMessageNotWritableException.protected @Nullable ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpRequestMethodNotSupportedException.protected @Nullable ResponseEntity<Object> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling of anyMaxUploadSizeExceededException.protected @Nullable ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofMethodArgumentNotValidException.protected @Nullable ResponseEntity<Object> handleMethodValidationException(MethodValidationException ex, HttpHeaders headers, HttpStatus status, WebRequest request) Customize the handling ofMethodValidationException.protected @Nullable ResponseEntity<Object> handleMissingPathVariable(MissingPathVariableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofMissingPathVariableException.protected @Nullable ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofMissingServletRequestParameterException.protected @Nullable ResponseEntity<Object> handleMissingServletRequestPart(MissingServletRequestPartException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofMissingServletRequestPartException.protected @Nullable ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofNoHandlerFoundException.protected @Nullable ResponseEntity<Object> handleNoResourceFoundException(NoResourceFoundException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofNoResourceFoundException.protected @Nullable ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofServletRequestBindingException.protected @Nullable ResponseEntity<Object> handleTypeMismatch(TypeMismatchException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofTypeMismatchException.voidsetMessageSource(MessageSource messageSource) Set theMessageSourcethat this object runs in.
- 
Field Details- 
PAGE_NOT_FOUND_LOG_CATEGORYLog category to use when no mapped handler is found for a request.- See Also:
 
- 
pageNotFoundLoggerprotected static final org.apache.commons.logging.Log pageNotFoundLoggerSpecific logger to use when no mapped handler is found for a request.- See Also:
 
- 
loggerprotected final org.apache.commons.logging.Log loggerCommon logger for use in subclasses.
 
- 
- 
Constructor Details- 
ResponseEntityExceptionHandlerpublic ResponseEntityExceptionHandler()
 
- 
- 
Method Details- 
setMessageSourceDescription copied from interface:MessageSourceAwareSet theMessageSourcethat 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:
- setMessageSourcein interface- MessageSourceAware
- Parameters:
- messageSource- message source to be used by this object
 
- 
getMessageSourceGet theMessageSourcethat 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,AsyncRequestNotUsableException.class}) public final @Nullable 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 handle
- request- the current request
- Throws:
- Exception
 
- 
handleHttpRequestMethodNotSupportedprotected @Nullable ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpRequestMethodNotSupportedException.This method logs a warning and delegates to handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
 
- 
handleHttpMediaTypeNotSupportedprotected @Nullable ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpMediaTypeNotSupportedException.This method delegates to handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
 
- 
handleHttpMediaTypeNotAcceptableprotected @Nullable ResponseEntity<Object> handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpMediaTypeNotAcceptableException.This method delegates to handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
 
- 
handleMissingPathVariableprotected @Nullable ResponseEntity<Object> handleMissingPathVariable(MissingPathVariableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofMissingPathVariableException.This method delegates to handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
- Since:
- 4.2
 
- 
handleMissingServletRequestParameterprotected @Nullable ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofMissingServletRequestParameterException.This method delegates to handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
 
- 
handleMissingServletRequestPartprotected @Nullable ResponseEntity<Object> handleMissingServletRequestPart(MissingServletRequestPartException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofMissingServletRequestPartException.This method delegates to handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
 
- 
handleServletRequestBindingExceptionprotected @Nullable ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofServletRequestBindingException.This method delegates to handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
 
- 
handleMethodArgumentNotValidprotected @Nullable ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofMethodArgumentNotValidException.This method delegates to handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
 
- 
handleHandlerMethodValidationExceptionprotected @Nullable ResponseEntity<Object> handleHandlerMethodValidationException(HandlerMethodValidationException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHandlerMethodValidationException.This method delegates to handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
- Since:
- 6.1
 
- 
handleNoHandlerFoundExceptionprotected @Nullable ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofNoHandlerFoundException.This method delegates to handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
- Since:
- 4.0
 
- 
handleNoResourceFoundExceptionprotected @Nullable ResponseEntity<Object> handleNoResourceFoundException(NoResourceFoundException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofNoResourceFoundException.This method delegates to handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
- Since:
- 6.1
 
- 
handleAsyncRequestTimeoutExceptionprotected @Nullable ResponseEntity<Object> handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofAsyncRequestTimeoutException.This method delegates to handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
- Since:
- 4.2.8
 
- 
handleErrorResponseExceptionprotected @Nullable ResponseEntity<Object> handleErrorResponseException(ErrorResponseException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling of anyErrorResponseException.This method delegates to handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
- Since:
- 6.0
 
- 
handleMaxUploadSizeExceededExceptionprotected @Nullable ResponseEntity<Object> handleMaxUploadSizeExceededException(MaxUploadSizeExceededException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling of anyMaxUploadSizeExceededException.This method delegates to handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
- Since:
- 6.1
 
- 
handleConversionNotSupportedprotected @Nullable ResponseEntity<Object> handleConversionNotSupported(ConversionNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofConversionNotSupportedException.By default this method creates a ProblemDetailwith the status and a short detail message, and also looks up an override for the detail viaMessageSource, before delegating tohandleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
 
- 
handleTypeMismatchprotected @Nullable ResponseEntity<Object> handleTypeMismatch(TypeMismatchException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofTypeMismatchException.By default this method creates a ProblemDetailwith the status and a short detail message, and also looks up an override for the detail viaMessageSource, before delegating tohandleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
 
- 
handleHttpMessageNotReadableprotected @Nullable ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpMessageNotReadableException.By default this method creates a ProblemDetailwith the status and a short detail message, and also looks up an override for the detail viaMessageSource, before delegating tohandleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
 
- 
handleHttpMessageNotWritableprotected @Nullable ResponseEntity<Object> handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the handling ofHttpMessageNotWritableException.By default this method creates a ProblemDetailwith the status and a short detail message, and also looks up an override for the detail viaMessageSource, before delegating tohandleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
 
- 
handleMethodValidationExceptionprotected @Nullable ResponseEntity<Object> handleMethodValidationException(MethodValidationException ex, HttpHeaders headers, HttpStatus status, WebRequest request) Customize the handling ofMethodValidationException.By default this method creates a ProblemDetailwith the status and a short detail message, and also looks up an override for the detail viaMessageSource, before delegating tohandleExceptionInternal(Exception, Object, HttpHeaders, HttpStatusCode, 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
- Since:
- 6.1
 
- 
handleAsyncRequestNotUsableExceptionprotected @Nullable ResponseEntity<Object> handleAsyncRequestNotUsableException(AsyncRequestNotUsableException ex, WebRequest request) Customize the handling ofAsyncRequestNotUsableException.By default, return nullsince the response is not usable.- Parameters:
- ex- the exception to handle
- request- the current request
- Returns:
- a ResponseEntityfor the response to use, possiblynullwhen the response is already committed
- Since:
- 6.2
 
- 
createProblemDetailprotected ProblemDetail createProblemDetail(Exception ex, HttpStatusCode status, String defaultDetail, @Nullable String detailMessageCode, Object @Nullable [] detailMessageArguments, WebRequest request) Convenience method to create aProblemDetailfor any exception that doesn't implementErrorResponse, also performing aMessageSourcelookup 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- nullthen- 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 ProblemDetailinstance
- Since:
- 6.0
 
- 
handleExceptionInternalprotected @Nullable 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 nullif response is already committed
- set the "jakarta.servlet.error.exception"request attribute if the response status is 500 (INTERNAL_SERVER_ERROR).
- extract the bodyfromErrorResponseexceptions, if thebodyisnull.
 - 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 ResponseEntityfor the response to use, possiblynullwhen the response is already committed
 
- return 
- 
createResponseEntityprotected ResponseEntity<Object> createResponseEntity(@Nullable Object body, HttpHeaders headers, HttpStatusCode statusCode, WebRequest request) Create theResponseEntityto 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 ofProblemDetailas an extension ofProblemDetail.- 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 ResponseEntityinstance to use
- Since:
- 6.0
 
 
-