public abstract class ResponseEntityExceptionHandler extends Object
@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.
handleException(Exception, WebRequest)
,
DefaultHandlerExceptionResolver
Modifier and Type | Field and Description |
---|---|
protected Log |
logger
Common logger for use in subclasses.
|
static String |
PAGE_NOT_FOUND_LOG_CATEGORY
Log category to use when no mapped handler is found for a request.
|
protected static Log |
pageNotFoundLogger
Specific logger to use when no mapped handler is found for a request.
|
Constructor and Description |
---|
ResponseEntityExceptionHandler() |
Modifier and Type | Method and Description |
---|---|
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.
|
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.
|
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 request)
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.
|
public static final String PAGE_NOT_FOUND_LOG_CATEGORY
pageNotFoundLogger
,
Constant Field Valuesprotected static final Log pageNotFoundLogger
PAGE_NOT_FOUND_LOG_CATEGORY
protected final Log logger
@ExceptionHandler(value={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.bind.ServletRequestBindingException.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.web.bind.MethodArgumentNotValidException.class,org.springframework.web.multipart.support.MissingServletRequestPartException.class,org.springframework.validation.BindException.class,NoHandlerFoundException.class,org.springframework.web.context.request.async.AsyncRequestTimeoutException.class}) @Nullable public final org.springframework.http.ResponseEntity<Object> handleException(Exception ex, org.springframework.web.context.request.WebRequest request) throws Exception
ex
- the target exceptionrequest
- the current requestException
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)
This method logs a warning, sets the "Allow" header, and delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatus, org.springframework.web.context.request.WebRequest)
.
ex
- the exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current requestResponseEntity
instanceprotected 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)
This method sets the "Accept" header and delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatus, org.springframework.web.context.request.WebRequest)
.
ex
- the exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current requestResponseEntity
instanceprotected 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)
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)
.
ex
- the exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current requestResponseEntity
instanceprotected 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)
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)
.
ex
- the exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current requestResponseEntity
instanceprotected 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)
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)
.
ex
- the exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current requestResponseEntity
instanceprotected 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)
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)
.
ex
- the exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current requestResponseEntity
instanceprotected 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)
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)
.
ex
- the exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current requestResponseEntity
instanceprotected 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)
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)
.
ex
- the exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current requestResponseEntity
instanceprotected 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)
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)
.
ex
- the exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current requestResponseEntity
instanceprotected 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)
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)
.
ex
- the exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current requestResponseEntity
instanceprotected 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)
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)
.
ex
- the exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current requestResponseEntity
instanceprotected 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)
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)
.
ex
- the exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current requestResponseEntity
instanceprotected 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)
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)
.
ex
- the exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current requestResponseEntity
instanceprotected 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)
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)
.
ex
- the exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current requestResponseEntity
instance@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)
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)
.
ex
- the exceptionheaders
- the headers to be written to the responsestatus
- the selected response statuswebRequest
- the current requestResponseEntity
instanceprotected 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 request)
The default implementation sets the WebUtils.ERROR_EXCEPTION_ATTRIBUTE
request attribute and creates a ResponseEntity
from the given
body, headers, and status.
ex
- the exceptionbody
- the body for the responseheaders
- the headers for the responsestatus
- the response statusrequest
- the current request