@ControllerAdvice 在存在@Around 时不起作用
@ControllerAdvice not working when @Around is present
我正在使用 Spring 带 AOP 的 Boot 1.5.6。对于异常处理,我将@ControllerAdvice 与@ExceptionHandler 一起使用,对于记录方法entry/exit 和调用时间,我使用AOP 的@Around。但是当我在故意抛出 RuntimeException 后调用 REST URL 时,ExceptionHandler 没有被执行。如果我用 @Before 替换 @Around 那么它正在工作 fine.Any 建议...
更新:我已经从@Around 代码块中重新抛出异常,现在一切正常。但是推荐吗?
@Around
方面环绕着您的整个方法。如果您不在该方法中抛出异常,异常将被消耗,因此异常处理程序将不会启动。
这意味着您不应该在方面内捕获任何异常。如果你想记录方法的持续时间,你应该在 finally
块内进行:
@Around("execution(* com.xyz.example.MyController.*(..))")
public void aroundCalls(ProceedingJoinPoint joinPoint) throws Throwable {
logger.info("Before call");
try {
// This will throw "Throwable", so you'll have to add it to your method
joinPoint.proceed();
} finally {
logger.info("After call");
}
}
我正在使用 Spring 带 AOP 的 Boot 1.5.6。对于异常处理,我将@ControllerAdvice 与@ExceptionHandler 一起使用,对于记录方法entry/exit 和调用时间,我使用AOP 的@Around。但是当我在故意抛出 RuntimeException 后调用 REST URL 时,ExceptionHandler 没有被执行。如果我用 @Before 替换 @Around 那么它正在工作 fine.Any 建议...
更新:我已经从@Around 代码块中重新抛出异常,现在一切正常。但是推荐吗?
@Around
方面环绕着您的整个方法。如果您不在该方法中抛出异常,异常将被消耗,因此异常处理程序将不会启动。
这意味着您不应该在方面内捕获任何异常。如果你想记录方法的持续时间,你应该在 finally
块内进行:
@Around("execution(* com.xyz.example.MyController.*(..))")
public void aroundCalls(ProceedingJoinPoint joinPoint) throws Throwable {
logger.info("Before call");
try {
// This will throw "Throwable", so you'll have to add it to your method
joinPoint.proceed();
} finally {
logger.info("After call");
}
}