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