@ConditionalOnWebApplication
@ControllerAdvice
public class AopLogControllerExceptionHandler
extends org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
Spring Boot官方文档给出的示例是直接继承ResponseEntityExceptionHandler,但是此类中的方法
ResponseEntityExceptionHandler.handleException(Exception, WebRequest)
是final的, 无法进行覆盖 (不覆盖将会导致此方法上捕获的异常无法在使用AOP拦截),并且没有将异常输出到log中。通过重写方法
ResponseEntityExceptionHandler.handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)
,因为所有的异常处理最后一步都会走到这里,对外的错误异常统一输出,完美解决。
如果要开启此功能,EnableAopLogController
如果要禁用掉此功能,EnableAopLogController.enableAopLogControllerException()
ResponseEntityExceptionHandler,
EnableAopLogController| Modifier and Type | Field and Description |
|---|---|
protected MonitorControllerProperties |
properties |
| Constructor and Description |
|---|
AopLogControllerExceptionHandler() |
| Modifier and Type | Method and Description |
|---|---|
static org.springframework.http.HttpStatus |
getStatus(javax.servlet.http.HttpServletRequest request)
获取 HTTP 请求响应状态码
|
org.springframework.http.ResponseEntity<Object> |
handleException(Throwable ex,
javax.servlet.http.HttpServletRequest request)
不在页面暴露具体的异常信息
|
protected org.springframework.http.ResponseEntity<Object> |
handleExceptionInternal(Exception ex,
Object responseBody,
org.springframework.http.HttpHeaders headers,
org.springframework.http.HttpStatus status,
org.springframework.web.context.request.WebRequest request)
ResponseEntityExceptionHandler.handleException(Exception, WebRequest)
是final的, 无法进行覆盖 (不覆盖将会导致此方法上捕获的异常无法在使用AOP拦截),并且没有将异常输出到log中。通过重写方法
ResponseEntityExceptionHandler.handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)
,因为所有的异常处理最后一步都会走到这里,对外的错误异常统一输出,完美解决。 |
protected org.springframework.http.ResponseEntity<Object> |
outputException(Throwable ex,
org.springframework.http.HttpHeaders headers,
org.springframework.http.HttpStatus status,
javax.servlet.http.HttpServletRequest servletRequest)
对外的错误异常统一输出JSON字符串,并且包含traceId,方便跟踪错误日志
|
handleAsyncRequestTimeoutException, handleBindException, handleConversionNotSupported, handleException, handleHttpMediaTypeNotAcceptable, handleHttpMediaTypeNotSupported, handleHttpMessageNotReadable, handleHttpMessageNotWritable, handleHttpRequestMethodNotSupported, handleMethodArgumentNotValid, handleMissingPathVariable, handleMissingServletRequestParameter, handleMissingServletRequestPart, handleNoHandlerFoundException, handleServletRequestBindingException, handleTypeMismatch@Autowired protected MonitorControllerProperties properties
public static org.springframework.http.HttpStatus getStatus(javax.servlet.http.HttpServletRequest request)
request - HTTP 请求@ExceptionHandler(value=java.lang.Throwable.class) @ResponseBody public org.springframework.http.ResponseEntity<Object> handleException(Throwable ex, javax.servlet.http.HttpServletRequest request)
ex - 异常信息request - 客户端请求信息protected org.springframework.http.ResponseEntity<Object> handleExceptionInternal(Exception ex, Object responseBody, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, org.springframework.web.context.request.WebRequest request)
ResponseEntityExceptionHandler.handleException(Exception, WebRequest)
是final的, 无法进行覆盖 (不覆盖将会导致此方法上捕获的异常无法在使用AOP拦截),并且没有将异常输出到log中。通过重写方法
ResponseEntityExceptionHandler.handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)
,因为所有的异常处理最后一步都会走到这里,对外的错误异常统一输出,完美解决。handleExceptionInternal in class org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandlerprotected org.springframework.http.ResponseEntity<Object> outputException(Throwable ex, org.springframework.http.HttpHeaders headers, org.springframework.http.HttpStatus status, javax.servlet.http.HttpServletRequest servletRequest)
ex - 异常信息headers - 服务端返回的HttpHeaders信息status - 服务端返回的状态码servletRequest - 客户端请求信息Copyright © 2018. All rights reserved.