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 for handling standard
Spring MVC exceptions that returns a ResponseEntity to be written with
message converters. This is in contrast to
DefaultHandlerExceptionResolver which returns a ModelAndView instead.
If there is no need to write error content to the response body, or if using
view resolution, e.g. ContentNegotiatingViewResolver, then use
DefaultHandlerExceptionResolver instead.
Note that in order for an @ControllerAdvice sub-class to be
detected, ExceptionHandlerExceptionResolver must be configured.
DefaultHandlerExceptionResolver| Modifier and Type | Field and Description |
|---|---|
protected Log |
logger |
static 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 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> |
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> |
handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex,
org.springframework.http.HttpHeaders headers,
org.springframework.http.HttpStatus status,
org.springframework.web.context.request.WebRequest request)
Customize the response for NoSuchRequestHandlingMethodException.
|
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.
|
protected final Log logger
public static final String PAGE_NOT_FOUND_LOG_CATEGORY
pageNotFoundLogger,
Constant Field Valuesprotected static final Log pageNotFoundLogger
PAGE_NOT_FOUND_LOG_CATEGORY@ExceptionHandler(value={NoSuchRequestHandlingMethodException.class,org.springframework.web.HttpRequestMethodNotSupportedException.class,org.springframework.web.HttpMediaTypeNotSupportedException.class,org.springframework.web.HttpMediaTypeNotAcceptableException.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})
public final org.springframework.http.ResponseEntity<Object> handleException(Exception ex,
org.springframework.web.context.request.WebRequest request)
ex - the target exceptionrequest - the current requestprotected 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)
null by default.ex - the exceptionbody - the body to use for the responseheaders - the headers to be written to the responsestatus - the selected response statusrequest - the current requestprotected org.springframework.http.ResponseEntity<Object> handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request)
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, 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> handleHttpRequestMethodNotSupported(org.springframework.web.HttpRequestMethodNotSupportedException ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request)
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, 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)
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, 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)
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, 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)
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, 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)
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, 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)
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, 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)
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, 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)
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, 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)
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, 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)
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, 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)
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, 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)
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, 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)
handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest).ex - the exceptionheaders - the headers to be written to the responsestatus - the selected response statusrequest - the current requestResponseEntity instance