aop环绕通知下全局异常捕获失效

aop的环绕通知存在的情况下配置全局异常失去作用 ,主要是因为在环绕通知中将发生的异常没有抛出从而导致全局异常捕获不到.
解决方案就是讲aop环绕通知中的异常再次抛出
@Aspect
@Component
public class LogAspect {
    
    

    @Pointcut("@annotation(com.dh.platform.entity.LogAnnotate)")
    public void logPoint(){
    
    

    }

    @Around(value = "logPoint()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
    
    
        long startTime = 0,endTime = 0;
        TableOperateLog operateLog=finishTableOperateLog();
        String[] methods=joinPoint.getSignature().toLongString().split(" ");
        operateLog.setController(methods[methods.length-1].split("\\(")[0]);
        Object object = null;
        try {
    
    
            startTime=System.currentTimeMillis();
            object=joinPoint.proceed();
            CommonResponse commonResponse= HttpCommon.jsonToObject((String) object,CommonResponse.class);
            operateLog.setHttpStatusCode(200);
            String msg=null;
            if(commonResponse.getResult()!=1){
    
    
                operateLog.setHttpStatusCode(400);
                if(commonResponse.getResult()==-1){
    
    
                    msg="权限不足。";
                }
                if(commonResponse.getResult()==-88){
    
    
                    msg="登陆超时。";
                }
                if(commonResponse.getResult()>=0){
    
    
                    msg = commonResponse.getReasonInfo();
                }
            }else{
    
    
                msg=commonResponse.getReasonInfo();
            }
            operateLog.setMemo(msg);
            operateLog.setReturnData(JSON.toJSONString(commonResponse.getDatum()));
        } catch (Throwable throwable) {
    
    
            operateLog.setHttpStatusCode(400);
            operateLog.setMemo("接口返回数据异常。");
            // throw throwable  不加这行代码全局异常将失效
            throw throwable;
        }finally {
    
    
            endTime=System.currentTimeMillis();
            operateLog.setReturnTime(Tools.GetNowTime());
            operateLog.setTimeConsuming(Integer.parseInt(String.valueOf(endTime-startTime)));
            commonService.saveTabOperateLog(operateLog);
        }
        return object;
    }

猜你喜欢

转载自blog.csdn.net/qq_37262094/article/details/103532560