类 ResponseEntityExceptionHandler
- java.lang.Object
- org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
public abstract class ResponseEntityExceptionHandler extends Object
A convenient base class for@ControllerAdviceclasses that wish to provide centralized exception handling across all@RequestMappingmethods through@ExceptionHandlermethods.This base class provides an
@ExceptionHandlermethod for handling internal Spring MVC exceptions. This method returns aResponseEntityfor writing to the response with amessage converter, in contrast toDefaultHandlerExceptionResolverwhich returns aModelAndView.If there is no need to write error content to the response body, or when using view resolution (e.g., via
ContentNegotiatingViewResolver), thenDefaultHandlerExceptionResolveris good enough.Note that in order for an
@ControllerAdvicesubclass to be detected,ExceptionHandlerExceptionResolvermust be configured.- 从以下版本开始:
- 3.2
- 作者:
- Rossen Stoyanchev
- 另请参阅:
handleException(Exception, WebRequest),DefaultHandlerExceptionResolver
字段概要
字段 修饰符和类型 字段 说明 protected LogloggerCommon logger for use in subclasses.static StringPAGE_NOT_FOUND_LOG_CATEGORYLog category to use when no mapped handler is found for a request.protected static LogpageNotFoundLoggerSpecific logger to use when no mapped handler is found for a request.
构造器概要
构造器 构造器 说明 ResponseEntityExceptionHandler()
方法概要
字段详细资料
PAGE_NOT_FOUND_LOG_CATEGORY
public static final String PAGE_NOT_FOUND_LOG_CATEGORY
Log category to use when no mapped handler is found for a request.- 另请参阅:
pageNotFoundLogger, 常量字段值
pageNotFoundLogger
protected static final Log pageNotFoundLogger
Specific logger to use when no mapped handler is found for a request.
构造器详细资料
ResponseEntityExceptionHandler
public ResponseEntityExceptionHandler()
方法详细资料
handleException
@ExceptionHandler({NoSuchRequestHandlingMethodException.class,HttpRequestMethodNotSupportedException.class,HttpMediaTypeNotSupportedException.class,HttpMediaTypeNotAcceptableException.class,MissingPathVariableException.class,MissingServletRequestParameterException.class,ServletRequestBindingException.class,ConversionNotSupportedException.class,TypeMismatchException.class,HttpMessageNotReadableException.class,HttpMessageNotWritableException.class,MethodArgumentNotValidException.class,MissingServletRequestPartException.class,BindException.class,NoHandlerFoundException.class,AsyncRequestTimeoutException.class}) public final ResponseEntity<Object> handleException(Exception ex, WebRequest request) throws Exception
Provides handling for standard Spring MVC exceptions.- 参数:
ex- the target exceptionrequest- the current request- 抛出:
Exception
handleNoSuchRequestHandlingMethod
@Deprecated protected ResponseEntity<Object> handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex, HttpHeaders headers, HttpStatus status, WebRequest request)
已过时。as of 4.3, along withNoSuchRequestHandlingMethodExceptionCustomize the response for NoSuchRequestHandlingMethodException.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 request- 返回:
- a
ResponseEntityinstance
handleHttpRequestMethodNotSupported
protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request)
Customize the response for HttpRequestMethodNotSupportedException.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 request- 返回:
- a
ResponseEntityinstance
handleHttpMediaTypeNotSupported
protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request)
Customize the response for HttpMediaTypeNotSupportedException.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 request- 返回:
- a
ResponseEntityinstance
handleHttpMediaTypeNotAcceptable
protected ResponseEntity<Object> handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex, HttpHeaders headers, HttpStatus 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.HttpStatus, org.springframework.web.context.request.WebRequest).- 参数:
ex- the exceptionheaders- the headers to be written to the responsestatus- the selected response statusrequest- the current request- 返回:
- a
ResponseEntityinstance
handleMissingPathVariable
protected ResponseEntity<Object> handleMissingPathVariable(MissingPathVariableException ex, HttpHeaders headers, HttpStatus 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.HttpStatus, org.springframework.web.context.request.WebRequest).- 参数:
ex- the exceptionheaders- the headers to be written to the responsestatus- the selected response statusrequest- the current request- 返回:
- a
ResponseEntityinstance - 从以下版本开始:
- 4.2
handleMissingServletRequestParameter
protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex, HttpHeaders headers, HttpStatus 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.HttpStatus, org.springframework.web.context.request.WebRequest).- 参数:
ex- the exceptionheaders- the headers to be written to the responsestatus- the selected response statusrequest- the current request- 返回:
- a
ResponseEntityinstance
handleServletRequestBindingException
protected ResponseEntity<Object> handleServletRequestBindingException(ServletRequestBindingException ex, HttpHeaders headers, HttpStatus 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.HttpStatus, org.springframework.web.context.request.WebRequest).- 参数:
ex- the exceptionheaders- the headers to be written to the responsestatus- the selected response statusrequest- the current request- 返回:
- a
ResponseEntityinstance
handleConversionNotSupported
protected ResponseEntity<Object> handleConversionNotSupported(ConversionNotSupportedException ex, HttpHeaders headers, HttpStatus 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.HttpStatus, org.springframework.web.context.request.WebRequest).- 参数:
ex- the exceptionheaders- the headers to be written to the responsestatus- the selected response statusrequest- the current request- 返回:
- a
ResponseEntityinstance
handleTypeMismatch
protected ResponseEntity<Object> handleTypeMismatch(TypeMismatchException ex, HttpHeaders headers, HttpStatus 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.HttpStatus, org.springframework.web.context.request.WebRequest).- 参数:
ex- the exceptionheaders- the headers to be written to the responsestatus- the selected response statusrequest- the current request- 返回:
- a
ResponseEntityinstance
handleHttpMessageNotReadable
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus 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.HttpStatus, org.springframework.web.context.request.WebRequest).- 参数:
ex- the exceptionheaders- the headers to be written to the responsestatus- the selected response statusrequest- the current request- 返回:
- a
ResponseEntityinstance
handleHttpMessageNotWritable
protected ResponseEntity<Object> handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpHeaders headers, HttpStatus 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.HttpStatus, org.springframework.web.context.request.WebRequest).- 参数:
ex- the exceptionheaders- the headers to be written to the responsestatus- the selected response statusrequest- the current request- 返回:
- a
ResponseEntityinstance
handleMethodArgumentNotValid
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus 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.HttpStatus, org.springframework.web.context.request.WebRequest).- 参数:
ex- the exceptionheaders- the headers to be written to the responsestatus- the selected response statusrequest- the current request- 返回:
- a
ResponseEntityinstance
handleMissingServletRequestPart
protected ResponseEntity<Object> handleMissingServletRequestPart(MissingServletRequestPartException ex, HttpHeaders headers, HttpStatus 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.HttpStatus, org.springframework.web.context.request.WebRequest).- 参数:
ex- the exceptionheaders- the headers to be written to the responsestatus- the selected response statusrequest- the current request- 返回:
- a
ResponseEntityinstance
handleBindException
protected ResponseEntity<Object> handleBindException(BindException ex, HttpHeaders headers, HttpStatus 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.HttpStatus, org.springframework.web.context.request.WebRequest).- 参数:
ex- the exceptionheaders- the headers to be written to the responsestatus- the selected response statusrequest- the current request- 返回:
- a
ResponseEntityinstance
handleNoHandlerFoundException
protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatus 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.HttpStatus, org.springframework.web.context.request.WebRequest).- 参数:
ex- the exceptionheaders- the headers to be written to the responsestatus- the selected response statusrequest- the current request- 返回:
- a
ResponseEntityinstance - 从以下版本开始:
- 4.0
handleAsyncRequestTimeoutException
protected ResponseEntity<Object> handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatus 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.HttpStatus, org.springframework.web.context.request.WebRequest).- 参数:
ex- the exceptionheaders- the headers to be written to the responsestatus- the selected response statuswebRequest- the current request- 返回:
- a
ResponseEntityinstance - 从以下版本开始:
- 4.2.8
handleExceptionInternal
protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status, WebRequest request)
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.- 参数:
ex- the exceptionbody- the body for the responseheaders- the headers for the responsestatus- the response statusrequest- the current request