Class ResponseEntityExceptionHandler
- All Implemented Interfaces:
org.springframework.beans.factory.Aware,org.springframework.context.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
FieldsModifier 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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected org.springframework.http.ProblemDetailcreateProblemDetail(Exception ex, org.springframework.http.HttpStatusCode status, String defaultDetail, @Nullable String detailMessageCode, Object @Nullable [] detailMessageArguments, org.springframework.web.context.request.WebRequest request) Convenience method to create aProblemDetailfor any exception that doesn't implementErrorResponse, also performing aMessageSourcelookup for the "detail" field.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 theResponseEntityto use from the given body, headers, and statusCode.protected @Nullable org.springframework.context.MessageSourceGet theMessageSourcethat this exception handler uses.handleAsyncRequestNotUsableException(org.springframework.web.context.request.async.AsyncRequestNotUsableException ex, org.springframework.web.context.request.WebRequest request) Customize the handling ofAsyncRequestNotUsableException.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 ofAsyncRequestTimeoutException.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 ofConversionNotSupportedException.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 anyErrorResponseException.handleException(Exception ex, org.springframework.web.context.request.WebRequest request) Handle all exceptions raised within Spring MVC handling of the request.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 aResponseEntity.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 ofHandlerMethodValidationException.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 ofHttpMediaTypeNotAcceptableException.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 ofHttpMediaTypeNotSupportedException.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 ofHttpMessageNotReadableException.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 ofHttpMessageNotWritableException.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 ofHttpRequestMethodNotSupportedException.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 anyMaxUploadSizeExceededException.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 ofMethodArgumentNotValidException.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 ofMethodValidationException.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 ofMissingPathVariableException.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 ofMissingServletRequestParameterException.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 ofMissingServletRequestPartException.handleNoHandlerFoundException(NoHandlerFoundException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request) Customize the handling ofNoHandlerFoundException.handleNoResourceFoundException(NoResourceFoundException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatusCode status, org.springframework.web.context.request.WebRequest request) Customize the handling ofNoResourceFoundException.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 ofServletRequestBindingException.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 ofTypeMismatchException.voidsetMessageSource(org.springframework.context.MessageSource messageSource)
-
Field Details
-
PAGE_NOT_FOUND_LOG_CATEGORY
Log category to use when no mapped handler is found for a request.- See Also:
-
pageNotFoundLogger
protected static final org.apache.commons.logging.Log pageNotFoundLoggerSpecific logger to use when no mapped handler is found for a request.- See Also:
-
logger
protected final org.apache.commons.logging.Log loggerCommon logger for use in subclasses.
-
-
Constructor Details
-
ResponseEntityExceptionHandler
public ResponseEntityExceptionHandler()
-
-
Method Details
-
setMessageSource
public void setMessageSource(org.springframework.context.MessageSource messageSource) - Specified by:
setMessageSourcein interfaceorg.springframework.context.MessageSourceAware
-
getMessageSource
Get theMessageSourcethat 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}) public final @Nullable 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 handlerequest- the current request- Throws:
Exception
-
handleHttpRequestMethodNotSupported
protected @Nullable 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 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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleHttpMediaTypeNotSupported
protected @Nullable 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 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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleHttpMediaTypeNotAcceptable
protected @Nullable 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 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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleMissingPathVariable
protected @Nullable 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 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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed - Since:
- 4.2
-
handleMissingServletRequestParameter
protected @Nullable 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 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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleMissingServletRequestPart
protected @Nullable 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 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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleServletRequestBindingException
protected @Nullable 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 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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleMethodArgumentNotValid
protected @Nullable 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 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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleHandlerMethodValidationException
protected @Nullable 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 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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed - Since:
- 6.1
-
handleNoHandlerFoundException
protected @Nullable 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 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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed - Since:
- 4.0
-
handleNoResourceFoundException
protected @Nullable 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 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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed - Since:
- 6.1
-
handleAsyncRequestTimeoutException
protected @Nullable 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 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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed - Since:
- 4.2.8
-
handleErrorResponseException
protected @Nullable 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 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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed - Since:
- 6.0
-
handleMaxUploadSizeExceededException
protected @Nullable 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 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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed - Since:
- 6.1
-
handleConversionNotSupported
protected @Nullable 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 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(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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleTypeMismatch
protected @Nullable 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 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(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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleHttpMessageNotReadable
protected @Nullable 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 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(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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleHttpMessageNotWritable
protected @Nullable 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 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(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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleMethodValidationException
protected @Nullable 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 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(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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed - Since:
- 6.1
-
handleAsyncRequestNotUsableException
protected @Nullable org.springframework.http.ResponseEntity<Object> handleAsyncRequestNotUsableException(org.springframework.web.context.request.async.AsyncRequestNotUsableException ex, org.springframework.web.context.request.WebRequest request) Customize the handling ofAsyncRequestNotUsableException.By default, return
nullsince the response is not usable.- Parameters:
ex- the exception to handlerequest- the current request- Returns:
- a
ResponseEntityfor the response to use, possiblynullwhen 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, Object @Nullable [] detailMessageArguments, org.springframework.web.context.request.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 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; ifnullthenErrorResponse.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
ProblemDetailinstance - Since:
- 6.0
-
handleExceptionInternal
protected @Nullable 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 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 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
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
- return
-
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 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 responseheaders- the headers to use for the responsestatusCode- the status code to use for the responserequest- the current request- Returns:
- the
ResponseEntityinstance to use - Since:
- 6.0
-