类 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 Framework MVC exceptions. This method returns a ResponseEntity
for writing to the response with a message converter,
in contrast to
SimpleHandlerExceptionHandler 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 DefaultHandlerExceptionHandler is good enough.
Note that in order for an @ControllerAdvice subclass to be
detected, HandlerExceptionHandler must be configured.
- 从以下版本开始:
- 4.0 2022/3/2 18:12
- 作者:
- Rossen Stoyanchev, Harry Yang
- 另请参阅:
-
字段概要
字段修饰符和类型字段说明protected final cn.taketoday.logging.LoggerCommon logger for use in subclasses.static final StringLog category to use when no mapped handler is found for a request.protected static final cn.taketoday.logging.LoggerSpecific logger to use when no mapped handler is found for a request. -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明protected ResponseEntity<Object>createResponseEntity(Object body, HttpHeaders headers, HttpStatusCode statusCode, RequestContext request) Create theResponseEntityto use from the given body, headers, and statusCode.protected ResponseEntity<Object>handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofAsyncRequestTimeoutException.protected ResponseEntity<Object>handleBindException(cn.taketoday.validation.BindException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofBindException.protected ResponseEntity<Object>handleConversionNotSupported(cn.taketoday.beans.ConversionNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofConversionNotSupportedException.protected ResponseEntity<Object>handleErrorResponseException(ErrorResponseException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling of anyErrorResponseException.final ResponseEntity<Object>handleException(Exception ex, RequestContext request) Provides handling for standard Framework MVC exceptions.protected ResponseEntity<Object>handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatusCode statusCode, RequestContext request) Internal handler method that all others in this class delegate to, for common handling, and for the creation of aResponseEntity.protected ResponseEntity<Object>handleHandlerNotFoundException(HandlerNotFoundException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofHandlerNotFoundException.protected ResponseEntity<Object>handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofHttpMediaTypeNotAcceptableException.protected ResponseEntity<Object>handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofHttpMediaTypeNotSupportedException.protected ResponseEntity<Object>handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofHttpMessageNotReadableException.protected ResponseEntity<Object>handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofHttpMessageNotWritableException.protected ResponseEntity<Object>handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofHttpRequestMethodNotSupportedException.protected ResponseEntity<Object>handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofMethodArgumentNotValidException.protected ResponseEntity<Object>handleMissingPathVariable(MissingPathVariableException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofMissingPathVariableException.protected ResponseEntity<Object>handleMissingRequestParameter(MissingRequestParameterException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofMissingRequestParameterException.protected ResponseEntity<Object>handleMissingRequestPart(MissingRequestPartException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofMissingRequestPartException.protected ResponseEntity<Object>handleRequestBindingException(RequestBindingException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofRequestBindingException.protected ResponseEntity<Object>handleTypeMismatch(cn.taketoday.beans.TypeMismatchException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofTypeMismatchException.
-
字段详细资料
-
PAGE_NOT_FOUND_LOG_CATEGORY
Log category to use when no mapped handler is found for a request.- 另请参阅:
-
pageNotFoundLogger
protected static final cn.taketoday.logging.Logger pageNotFoundLoggerSpecific logger to use when no mapped handler is found for a request. -
logger
protected final cn.taketoday.logging.Logger loggerCommon logger for use in subclasses.
-
-
构造器详细资料
-
ResponseEntityExceptionHandler
public ResponseEntityExceptionHandler()
-
-
方法详细资料
-
handleException
@ExceptionHandler({HttpRequestMethodNotSupportedException.class,HttpMediaTypeNotSupportedException.class,HttpMediaTypeNotAcceptableException.class,MissingPathVariableException.class,MissingRequestParameterException.class,MissingRequestPartException.class,RequestBindingException.class,MethodArgumentNotValidException.class,HandlerNotFoundException.class,AsyncRequestTimeoutException.class,ErrorResponseException.class,cn.taketoday.beans.ConversionNotSupportedException.class,cn.taketoday.beans.TypeMismatchException.class,HttpMessageNotReadableException.class,HttpMessageNotWritableException.class,cn.taketoday.validation.BindException.class}) @Nullable public final ResponseEntity<Object> handleException(Exception ex, RequestContext request) throws Exception Provides handling for standard Framework MVC exceptions.- 参数:
ex- the target exceptionrequest- the current request- 抛出:
Exception
-
handleHttpRequestMethodNotSupported
@Nullable protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofHttpRequestMethodNotSupportedException.This method logs a warning and delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, cn.taketoday.http.HttpHeaders, cn.taketoday.http.HttpStatusCode, cn.taketoday.web.RequestContext).- 参数:
ex- the exception to handleheaders- the headers to use for the responsestatus- the status code to use for the responserequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleHttpMediaTypeNotSupported
@Nullable protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofHttpMediaTypeNotSupportedException.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, cn.taketoday.http.HttpHeaders, cn.taketoday.http.HttpStatusCode, cn.taketoday.web.RequestContext).- 参数:
ex- the exception to handleheaders- the headers to use for the responsestatus- the status code to use for the responserequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleHttpMediaTypeNotAcceptable
@Nullable protected ResponseEntity<Object> handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofHttpMediaTypeNotAcceptableException.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, cn.taketoday.http.HttpHeaders, cn.taketoday.http.HttpStatusCode, cn.taketoday.web.RequestContext).- 参数:
ex- the exception to handleheaders- the headers to use for the responsestatus- the status code to use for the responserequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleMissingPathVariable
@Nullable protected ResponseEntity<Object> handleMissingPathVariable(MissingPathVariableException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofMissingPathVariableException.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, cn.taketoday.http.HttpHeaders, cn.taketoday.http.HttpStatusCode, cn.taketoday.web.RequestContext).- 参数:
ex- the exception to handleheaders- the headers to use for the responsestatus- the status code to use for the responserequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleMissingRequestParameter
@Nullable protected ResponseEntity<Object> handleMissingRequestParameter(MissingRequestParameterException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofMissingRequestParameterException.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, cn.taketoday.http.HttpHeaders, cn.taketoday.http.HttpStatusCode, cn.taketoday.web.RequestContext).- 参数:
ex- the exception to handleheaders- the headers to use for the responsestatus- the status code to use for the responserequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleMissingRequestPart
@Nullable protected ResponseEntity<Object> handleMissingRequestPart(MissingRequestPartException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofMissingRequestPartException.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, cn.taketoday.http.HttpHeaders, cn.taketoday.http.HttpStatusCode, cn.taketoday.web.RequestContext).- 参数:
ex- the exception to handleheaders- the headers to use for the responsestatus- the status code to use for the responserequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleRequestBindingException
@Nullable protected ResponseEntity<Object> handleRequestBindingException(RequestBindingException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofRequestBindingException.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, cn.taketoday.http.HttpHeaders, cn.taketoday.http.HttpStatusCode, cn.taketoday.web.RequestContext).- 参数:
ex- the exception to handleheaders- the headers to use for the responsestatus- the status code to use for the responserequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleMethodArgumentNotValid
@Nullable protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofMethodArgumentNotValidException.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, cn.taketoday.http.HttpHeaders, cn.taketoday.http.HttpStatusCode, cn.taketoday.web.RequestContext).- 参数:
ex- the exception to handleheaders- the headers to be written to the responsestatus- the selected response statusrequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleHandlerNotFoundException
@Nullable protected ResponseEntity<Object> handleHandlerNotFoundException(HandlerNotFoundException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofHandlerNotFoundException.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, cn.taketoday.http.HttpHeaders, cn.taketoday.http.HttpStatusCode, cn.taketoday.web.RequestContext).- 参数:
ex- the exception to handleheaders- the headers to use for the responsestatus- the status code to use for the responserequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed - 从以下版本开始:
- 4.0
-
handleAsyncRequestTimeoutException
@Nullable protected ResponseEntity<Object> handleAsyncRequestTimeoutException(AsyncRequestTimeoutException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofAsyncRequestTimeoutException.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, cn.taketoday.http.HttpHeaders, cn.taketoday.http.HttpStatusCode, cn.taketoday.web.RequestContext).- 参数:
ex- the exception to handleheaders- the headers to use for the responsestatus- the status code to use for the responserequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleErrorResponseException
@Nullable protected ResponseEntity<Object> handleErrorResponseException(ErrorResponseException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling of anyErrorResponseException.This method delegates to
handleExceptionInternal(java.lang.Exception, java.lang.Object, cn.taketoday.http.HttpHeaders, cn.taketoday.http.HttpStatusCode, cn.taketoday.web.RequestContext).- 参数:
ex- the exception to handleheaders- the headers to use for the responsestatus- the status code to use for the responserequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleConversionNotSupported
@Nullable protected ResponseEntity<Object> handleConversionNotSupported(cn.taketoday.beans.ConversionNotSupportedException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofConversionNotSupportedException.By default this method creates a
ProblemDetailwith the status and a short detail message, and then delegates tohandleExceptionInternal(java.lang.Exception, java.lang.Object, cn.taketoday.http.HttpHeaders, cn.taketoday.http.HttpStatusCode, cn.taketoday.web.RequestContext).- 参数:
ex- the exception to handleheaders- the headers to use for the responsestatus- the status code to use for the responserequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleTypeMismatch
@Nullable protected ResponseEntity<Object> handleTypeMismatch(cn.taketoday.beans.TypeMismatchException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofTypeMismatchException.By default this method creates a
ProblemDetailwith the status and a short detail message, and then delegates tohandleExceptionInternal(java.lang.Exception, java.lang.Object, cn.taketoday.http.HttpHeaders, cn.taketoday.http.HttpStatusCode, cn.taketoday.web.RequestContext).- 参数:
ex- the exception to handleheaders- the headers to use for the responsestatus- the status code to use for the responserequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleHttpMessageNotReadable
@Nullable protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofHttpMessageNotReadableException.By default this method creates a
ProblemDetailwith the status and a short detail message, and then delegates tohandleExceptionInternal(java.lang.Exception, java.lang.Object, cn.taketoday.http.HttpHeaders, cn.taketoday.http.HttpStatusCode, cn.taketoday.web.RequestContext).- 参数:
ex- the exception to handleheaders- the headers to use for the responsestatus- the status code to use for the responserequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleHttpMessageNotWritable
@Nullable protected ResponseEntity<Object> handleHttpMessageNotWritable(HttpMessageNotWritableException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofHttpMessageNotWritableException.By default this method creates a
ProblemDetailwith the status and a short detail message, and then delegates tohandleExceptionInternal(java.lang.Exception, java.lang.Object, cn.taketoday.http.HttpHeaders, cn.taketoday.http.HttpStatusCode, cn.taketoday.web.RequestContext).- 参数:
ex- the exception to handleheaders- the headers to use for the responsestatus- the status code to use for the responserequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleBindException
@Nullable protected ResponseEntity<Object> handleBindException(cn.taketoday.validation.BindException ex, HttpHeaders headers, HttpStatusCode status, RequestContext request) Customize the handling ofBindException.By default this method creates a
ProblemDetailwith the status and a short detail message, and then delegates tohandleExceptionInternal(java.lang.Exception, java.lang.Object, cn.taketoday.http.HttpHeaders, cn.taketoday.http.HttpStatusCode, cn.taketoday.web.RequestContext).- 参数:
ex- the exception to handleheaders- the headers to use for the responsestatus- the status code to use for the responserequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
-
handleExceptionInternal
@Nullable protected ResponseEntity<Object> handleExceptionInternal(Exception ex, @Nullable Object body, HttpHeaders headers, HttpStatusCode statusCode, RequestContext request) Internal handler method that all others in this class delegate to, for common handling, and for the creation of aResponseEntity.The default implementation does the following:
- return
nullif response is already committed - set the
"jakarta.servlet.error.exception"request attribute if the response status is 500 (INTERNAL_SERVER_ERROR). - extract the
bodyfromErrorResponseexceptions, if thebodyisnull.
- 参数:
ex- the exception to handlebody- the body to use for the responseheaders- the headers to use for the responsestatusCode- the status code to use for the responserequest- the current request- 返回:
- a
ResponseEntityfor the response to use, possiblynullwhen the response is already committed
- return
-
createResponseEntity
protected ResponseEntity<Object> createResponseEntity(@Nullable Object body, HttpHeaders headers, HttpStatusCode statusCode, RequestContext request) Create theResponseEntityto use from the given body, headers, and statusCode. Subclasses can override this method to inspect and possibly modify the body, headers, or statusCode, e.g. to re-create an instance ofProblemDetailas an extension ofProblemDetail.- 参数:
body- the body to use for the responseheaders- the headers to use for the responsestatusCode- the status code to use for the responserequest- the current request- 返回:
- the
ResponseEntityinstance to use
-