Class ResponseEntityExceptionHandler
@ControllerAdvice
classes
that wish to provide centralized exception handling across all
@RequestMapping
methods through @ExceptionHandler
methods.
This base class provides an @ExceptionHandler
method for handling
internal Spring MVC exceptions. This method returns a ResponseEntity
for writing to the response with a message converter
,
in contrast to
DefaultHandlerExceptionResolver
which returns a
ModelAndView
.
If there is no need to write error content to the response body, or when
using view resolution (e.g., via ContentNegotiatingViewResolver
),
then DefaultHandlerExceptionResolver
is good enough.
Note that in order for an @ControllerAdvice
subclass to be
detected, ExceptionHandlerExceptionResolver
must be configured.
- Since:
- 3.2
- Author:
- Rossen Stoyanchev
- See Also:
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected ResponseEntity<Object>
handleAsyncRequestTimeoutException
(AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatusCode status, WebRequest webRequest) Customize the response for AsyncRequestTimeoutException.protected ResponseEntity<Object>
handleBindException
(BindException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for BindException.protected ResponseEntity<Object>
handleConversionNotSupported
(ConversionNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for ConversionNotSupportedException.final ResponseEntity<Object>
handleException
(Exception ex, WebRequest request) Provides handling for standard Spring MVC exceptions.protected ResponseEntity<Object>
handleExceptionInternal
(Exception ex, Object body, HttpHeaders headers, HttpStatusCode statusCode, WebRequest webRequest) A single place to customize the response body of all exception types.protected ResponseEntity<Object>
handleHttpMediaTypeNotAcceptable
(HttpMediaTypeNotAcceptableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for HttpMediaTypeNotAcceptableException.protected ResponseEntity<Object>
handleHttpMediaTypeNotSupported
(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for HttpMediaTypeNotSupportedException.protected ResponseEntity<Object>
handleHttpMessageNotReadable
(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for HttpMessageNotReadableException.protected ResponseEntity<Object>
handleHttpMessageNotWritable
(HttpMessageNotWritableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for HttpMessageNotWritableException.protected ResponseEntity<Object>
handleHttpRequestMethodNotSupported
(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for HttpRequestMethodNotSupportedException.protected ResponseEntity<Object>
handleMethodArgumentNotValid
(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for MethodArgumentNotValidException.protected ResponseEntity<Object>
handleMissingPathVariable
(MissingPathVariableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for MissingPathVariableException.protected ResponseEntity<Object>
handleMissingServletRequestParameter
(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for MissingServletRequestParameterException.protected ResponseEntity<Object>
handleMissingServletRequestPart
(MissingServletRequestPartException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for MissingServletRequestPartException.protected ResponseEntity<Object>
handleNoHandlerFoundException
(NoHandlerFoundException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for NoHandlerFoundException.protected ResponseEntity<Object>
handleServletRequestBindingException
(ServletRequestBindingException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for ServletRequestBindingException.protected ResponseEntity<Object>
handleTypeMismatch
(TypeMismatchException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for TypeMismatchException.
-
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
-
handleException
@ExceptionHandler({HttpRequestMethodNotSupportedException.class,HttpMediaTypeNotSupportedException.class,HttpMediaTypeNotAcceptableException.class,MissingPathVariableException.class,MissingServletRequestParameterException.class,MissingServletRequestPartException.class,ServletRequestBindingException.class,MethodArgumentNotValidException.class,NoHandlerFoundException.class,AsyncRequestTimeoutException.class,ErrorResponseException.class,ConversionNotSupportedException.class,TypeMismatchException.class,HttpMessageNotReadableException.class,HttpMessageNotWritableException.class,BindException.class}) @Nullable public final ResponseEntity<Object> handleException(Exception ex, WebRequest request) throws Exception Provides handling for standard Spring MVC exceptions.- Parameters:
ex
- the target exceptionrequest
- the current request- Throws:
Exception
-
handleHttpRequestMethodNotSupported
@Nullable protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for 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 exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current request- Returns:
ResponseEntity
ornull
if response is committed
-
handleHttpMediaTypeNotSupported
@Nullable protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for 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 exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current request- Returns:
ResponseEntity
ornull
if response is committed
-
handleHttpMediaTypeNotAcceptable
@Nullable protected ResponseEntity<Object> handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for 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 exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current request- Returns:
ResponseEntity
ornull
if response is committed
-
handleMissingPathVariable
@Nullable protected ResponseEntity<Object> handleMissingPathVariable(MissingPathVariableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for 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 exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current request- Returns:
ResponseEntity
ornull
if response is committed- Since:
- 4.2
-
handleMissingServletRequestParameter
@Nullable protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for 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 exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current request- Returns:
ResponseEntity
ornull
if response is committed
-
handleMissingServletRequestPart
@Nullable protected ResponseEntity<Object> handleMissingServletRequestPart(MissingServletRequestPartException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for 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 exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current request- Returns:
ResponseEntity
ornull
if response is committed
-
handleServletRequestBindingException
@Nullable protected ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for 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 exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current request- Returns:
ResponseEntity
ornull
if response is committed
-
handleMethodArgumentNotValid
@Nullable protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for 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 exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current request- Returns:
ResponseEntity
ornull
if response is committed
-
handleNoHandlerFoundException
@Nullable protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for 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 exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current request- Returns:
ResponseEntity
ornull
if response is committed- Since:
- 4.0
-
handleAsyncRequestTimeoutException
@Nullable protected ResponseEntity<Object> handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatusCode status, WebRequest webRequest) Customize the response for 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 exceptionheaders
- the headers to be written to the responsestatus
- the selected response statuswebRequest
- the current request- Returns:
ResponseEntity
ornull
if response is committed- Since:
- 4.2.8
-
handleConversionNotSupported
@Nullable protected ResponseEntity<Object> handleConversionNotSupported(ConversionNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for ConversionNotSupportedException.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 exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current request- Returns:
ResponseEntity
ornull
if response is committed
-
handleTypeMismatch
@Nullable protected ResponseEntity<Object> handleTypeMismatch(TypeMismatchException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for TypeMismatchException.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 exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current request- Returns:
ResponseEntity
ornull
if response is committed
-
handleHttpMessageNotReadable
@Nullable protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for HttpMessageNotReadableException.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 exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current request- Returns:
ResponseEntity
ornull
if response is committed
-
handleHttpMessageNotWritable
@Nullable protected ResponseEntity<Object> handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for HttpMessageNotWritableException.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 exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current request- Returns:
ResponseEntity
ornull
if response is committed
-
handleBindException
@Nullable protected ResponseEntity<Object> handleBindException(BindException ex, HttpHeaders headers, HttpStatusCode status, WebRequest request) Customize the response for BindException.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 exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current request- Returns:
ResponseEntity
ornull
if response is committed
-
handleExceptionInternal
@Nullable protected ResponseEntity<Object> handleExceptionInternal(Exception ex, @Nullable Object body, HttpHeaders headers, HttpStatusCode statusCode, WebRequest webRequest) A single place to customize the response body of all exception types.The default implementation sets the
WebUtils.ERROR_EXCEPTION_ATTRIBUTE
request attribute and creates aResponseEntity
from the given body, headers, and status.- Parameters:
ex
- the exceptionbody
- the body for the responseheaders
- the headers for the responsestatusCode
- the response statuswebRequest
- the current request- Returns:
ResponseEntity
ornull
if response is committed
-