public abstract class ResponseEntityExceptionHandler
extends java.lang.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
sub-class to be
detected, ExceptionHandlerExceptionResolver
must be configured.
handleException(Exception, WebRequest)
,
DefaultHandlerExceptionResolver
Modifier and Type | Field and Description |
---|---|
protected Log |
logger |
static java.lang.String |
PAGE_NOT_FOUND_LOG_CATEGORY
Log category to use when no mapped handler is found for a request.
|
protected static Log |
pageNotFoundLogger
Additional logger to use when no mapped handler is found for a request.
|
Constructor and Description |
---|
ResponseEntityExceptionHandler() |
Modifier and Type | Method and Description |
---|---|
protected ResponseEntity<java.lang.Object> |
handleBindException(BindException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
Customize the response for BindException.
|
protected ResponseEntity<java.lang.Object> |
handleConversionNotSupported(ConversionNotSupportedException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
Customize the response for ConversionNotSupportedException.
|
ResponseEntity<java.lang.Object> |
handleException(java.lang.Exception ex,
WebRequest request)
Provides handling for standard Spring MVC exceptions.
|
protected ResponseEntity<java.lang.Object> |
handleExceptionInternal(java.lang.Exception ex,
java.lang.Object body,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
A single place to customize the response body of all Exception types.
|
protected ResponseEntity<java.lang.Object> |
handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
Customize the response for HttpMediaTypeNotAcceptableException.
|
protected ResponseEntity<java.lang.Object> |
handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
Customize the response for HttpMediaTypeNotSupportedException.
|
protected ResponseEntity<java.lang.Object> |
handleHttpMessageNotReadable(HttpMessageNotReadableException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
Customize the response for HttpMessageNotReadableException.
|
protected ResponseEntity<java.lang.Object> |
handleHttpMessageNotWritable(HttpMessageNotWritableException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
Customize the response for HttpMessageNotWritableException.
|
protected ResponseEntity<java.lang.Object> |
handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
Customize the response for HttpRequestMethodNotSupportedException.
|
protected ResponseEntity<java.lang.Object> |
handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
Customize the response for MethodArgumentNotValidException.
|
protected ResponseEntity<java.lang.Object> |
handleMissingPathVariable(MissingPathVariableException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
Customize the response for MissingPathVariableException.
|
protected ResponseEntity<java.lang.Object> |
handleMissingServletRequestParameter(MissingServletRequestParameterException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
Customize the response for MissingServletRequestParameterException.
|
protected ResponseEntity<java.lang.Object> |
handleMissingServletRequestPart(MissingServletRequestPartException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
Customize the response for MissingServletRequestPartException.
|
protected ResponseEntity<java.lang.Object> |
handleNoHandlerFoundException(NoHandlerFoundException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
Customize the response for NoHandlerFoundException.
|
protected ResponseEntity<java.lang.Object> |
handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
Customize the response for NoSuchRequestHandlingMethodException.
|
protected ResponseEntity<java.lang.Object> |
handleServletRequestBindingException(ServletRequestBindingException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
Customize the response for ServletRequestBindingException.
|
protected ResponseEntity<java.lang.Object> |
handleTypeMismatch(TypeMismatchException ex,
HttpHeaders headers,
HttpStatus status,
WebRequest request)
Customize the response for TypeMismatchException.
|
protected final Log logger
public static final java.lang.String PAGE_NOT_FOUND_LOG_CATEGORY
pageNotFoundLogger
,
Constant Field Valuesprotected static final Log pageNotFoundLogger
PAGE_NOT_FOUND_LOG_CATEGORY
@ExceptionHandler(value={<error>,<error>,<error>,<error>,<error>,<error>,<error>,ConversionNotSupportedException.class,TypeMismatchException.class,HttpMessageNotReadableException.class,HttpMessageNotWritableException.class,MethodArgumentNotValidException.class,<error>,BindException.class,<error>}) public final ResponseEntity<java.lang.Object> handleException(java.lang.Exception ex, WebRequest request)
ex
- the target exceptionrequest
- the current requestprotected ResponseEntity<java.lang.Object> handleExceptionInternal(java.lang.Exception ex, java.lang.Object body, HttpHeaders headers, HttpStatus status, 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 requestprotected ResponseEntity<java.lang.Object> handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex, HttpHeaders headers, HttpStatus status, WebRequest request)
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)
.
ex
- the exceptionheaders
- the headers to be written to the responsestatus
- the selected response statusrequest
- the current requestResponseEntity
instanceprotected ResponseEntity<java.lang.Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, 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 ResponseEntity<java.lang.Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatus status, 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 ResponseEntity<java.lang.Object> handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex, HttpHeaders headers, HttpStatus status, 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 ResponseEntity<java.lang.Object> handleMissingPathVariable(MissingPathVariableException ex, HttpHeaders headers, HttpStatus status, 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 ResponseEntity<java.lang.Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus status, 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 ResponseEntity<java.lang.Object> handleServletRequestBindingException(ServletRequestBindingException ex, HttpHeaders headers, HttpStatus status, 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 ResponseEntity<java.lang.Object> handleConversionNotSupported(ConversionNotSupportedException ex, HttpHeaders headers, HttpStatus status, 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 ResponseEntity<java.lang.Object> handleTypeMismatch(TypeMismatchException ex, HttpHeaders headers, HttpStatus status, 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 ResponseEntity<java.lang.Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, 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 ResponseEntity<java.lang.Object> handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpHeaders headers, HttpStatus status, 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 ResponseEntity<java.lang.Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, 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 ResponseEntity<java.lang.Object> handleMissingServletRequestPart(MissingServletRequestPartException ex, HttpHeaders headers, HttpStatus status, 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 ResponseEntity<java.lang.Object> handleBindException(BindException ex, HttpHeaders headers, HttpStatus status, 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 ResponseEntity<java.lang.Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, 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