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
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected org.springframework.http.ResponseEntity<Object>handleAsyncRequestTimeoutException(org.springframework.web.context.request.async.AsyncRequestTimeoutException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest webRequest) Customize the response for AsyncRequestTimeoutException.protected org.springframework.http.ResponseEntity<Object>handleBindException(org.springframework.validation.BindException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request) Customize the response for BindException.protected org.springframework.http.ResponseEntity<Object>handleConversionNotSupported(org.springframework.beans.ConversionNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request) Customize the response for ConversionNotSupportedException.final org.springframework.http.ResponseEntity<Object>handleException(Exception ex, org.springframework.web.context.request.WebRequest request) Provides handling for standard Spring MVC exceptions.protected org.springframework.http.ResponseEntity<Object>handleExceptionInternal(Exception ex, Object body, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest webRequest) A single place to customize the response body of all exception types.protected org.springframework.http.ResponseEntity<Object>handleHttpMediaTypeNotAcceptable(org.springframework.web.HttpMediaTypeNotAcceptableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request) Customize the response for HttpMediaTypeNotAcceptableException.protected org.springframework.http.ResponseEntity<Object>handleHttpMediaTypeNotSupported(org.springframework.web.HttpMediaTypeNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request) Customize the response for HttpMediaTypeNotSupportedException.protected org.springframework.http.ResponseEntity<Object>handleHttpMessageNotReadable(org.springframework.http.converter.HttpMessageNotReadableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request) Customize the response for HttpMessageNotReadableException.protected org.springframework.http.ResponseEntity<Object>handleHttpMessageNotWritable(org.springframework.http.converter.HttpMessageNotWritableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request) Customize the response for HttpMessageNotWritableException.protected org.springframework.http.ResponseEntity<Object>handleHttpRequestMethodNotSupported(org.springframework.web.HttpRequestMethodNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request) Customize the response for HttpRequestMethodNotSupportedException.protected org.springframework.http.ResponseEntity<Object>handleMethodArgumentNotValid(org.springframework.web.bind.MethodArgumentNotValidException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request) Customize the response for MethodArgumentNotValidException.protected org.springframework.http.ResponseEntity<Object>handleMissingPathVariable(org.springframework.web.bind.MissingPathVariableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request) Customize the response for MissingPathVariableException.protected org.springframework.http.ResponseEntity<Object>handleMissingServletRequestParameter(org.springframework.web.bind.MissingServletRequestParameterException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request) Customize the response for MissingServletRequestParameterException.protected org.springframework.http.ResponseEntity<Object>handleMissingServletRequestPart(org.springframework.web.multipart.support.MissingServletRequestPartException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request) Customize the response for MissingServletRequestPartException.protected org.springframework.http.ResponseEntity<Object>handleNoHandlerFoundException(NoHandlerFoundException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request) Customize the response for NoHandlerFoundException.protected org.springframework.http.ResponseEntity<Object>handleServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request) Customize the response for ServletRequestBindingException.protected org.springframework.http.ResponseEntity<Object>handleTypeMismatch(org.springframework.beans.TypeMismatchException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.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({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,NoHandlerFoundException.class,org.springframework.web.context.request.async.AsyncRequestTimeoutException.class,org.springframework.web.ErrorResponseException.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.BindException.class}) @Nullable public final org.springframework.http.ResponseEntity<Object> handleException(Exception ex, org.springframework.web.context.request.WebRequest request) throws Exception Provides handling for standard Spring MVC exceptions.- Parameters:
ex- the target exceptionrequest- the current request- Throws:
Exception
-
handleHttpRequestMethodNotSupported
@Nullable protected org.springframework.http.ResponseEntity<Object> handleHttpRequestMethodNotSupported(org.springframework.web.HttpRequestMethodNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.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.HttpStatus, 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:
ResponseEntityornullif response is committed
-
handleHttpMediaTypeNotSupported
@Nullable protected org.springframework.http.ResponseEntity<Object> handleHttpMediaTypeNotSupported(org.springframework.web.HttpMediaTypeNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.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.HttpStatus, 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:
ResponseEntityornullif response is committed
-
handleHttpMediaTypeNotAcceptable
@Nullable protected org.springframework.http.ResponseEntity<Object> handleHttpMediaTypeNotAcceptable(org.springframework.web.HttpMediaTypeNotAcceptableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.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.HttpStatus, 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:
ResponseEntityornullif response is committed
-
handleMissingPathVariable
@Nullable protected org.springframework.http.ResponseEntity<Object> handleMissingPathVariable(org.springframework.web.bind.MissingPathVariableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.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.HttpStatus, 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:
ResponseEntityornullif response is committed- Since:
- 4.2
-
handleMissingServletRequestParameter
@Nullable protected org.springframework.http.ResponseEntity<Object> handleMissingServletRequestParameter(org.springframework.web.bind.MissingServletRequestParameterException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.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.HttpStatus, 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:
ResponseEntityornullif response is committed
-
handleMissingServletRequestPart
@Nullable protected org.springframework.http.ResponseEntity<Object> handleMissingServletRequestPart(org.springframework.web.multipart.support.MissingServletRequestPartException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.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.HttpStatus, 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:
ResponseEntityornullif response is committed
-
handleServletRequestBindingException
@Nullable protected org.springframework.http.ResponseEntity<Object> handleServletRequestBindingException(org.springframework.web.bind.ServletRequestBindingException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.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.HttpStatus, 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:
ResponseEntityornullif response is committed
-
handleMethodArgumentNotValid
@Nullable protected org.springframework.http.ResponseEntity<Object> handleMethodArgumentNotValid(org.springframework.web.bind.MethodArgumentNotValidException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.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.HttpStatus, 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:
ResponseEntityornullif response is committed
-
handleNoHandlerFoundException
@Nullable protected org.springframework.http.ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.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.HttpStatus, 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:
ResponseEntityornullif response is committed- Since:
- 4.0
-
handleAsyncRequestTimeoutException
@Nullable protected org.springframework.http.ResponseEntity<Object> handleAsyncRequestTimeoutException(org.springframework.web.context.request.async.AsyncRequestTimeoutException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.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.HttpStatus, 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:
ResponseEntityornullif response is committed- Since:
- 4.2.8
-
handleConversionNotSupported
@Nullable protected org.springframework.http.ResponseEntity<Object> handleConversionNotSupported(org.springframework.beans.ConversionNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.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.HttpStatus, 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:
ResponseEntityornullif response is committed
-
handleTypeMismatch
@Nullable protected org.springframework.http.ResponseEntity<Object> handleTypeMismatch(org.springframework.beans.TypeMismatchException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.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.HttpStatus, 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:
ResponseEntityornullif response is committed
-
handleHttpMessageNotReadable
@Nullable protected org.springframework.http.ResponseEntity<Object> handleHttpMessageNotReadable(org.springframework.http.converter.HttpMessageNotReadableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.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.HttpStatus, 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:
ResponseEntityornullif response is committed
-
handleHttpMessageNotWritable
@Nullable protected org.springframework.http.ResponseEntity<Object> handleHttpMessageNotWritable(org.springframework.http.converter.HttpMessageNotWritableException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.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.HttpStatus, 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:
ResponseEntityornullif response is committed
-
handleBindException
@Nullable protected org.springframework.http.ResponseEntity<Object> handleBindException(org.springframework.validation.BindException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.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.HttpStatus, 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:
ResponseEntityornullif response is committed
-
handleExceptionInternal
@Nullable protected org.springframework.http.ResponseEntity<Object> handleExceptionInternal(Exception ex, @Nullable Object body, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest webRequest) A single place to customize the response body of all exception types.The default implementation sets the
WebUtils.ERROR_EXCEPTION_ATTRIBUTErequest attribute and creates aResponseEntityfrom the given body, headers, and status.- Parameters:
ex- the exceptionbody- the body for the responseheaders- the headers for the responsestatus- the response statuswebRequest- the current request- Returns:
ResponseEntityornullif response is committed
-