It is illegal to call this method if the current request is not in asynchronous mode

nested exception is java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)] with root cause

MethodSignature methodSignature = ((MethodSignature) joinPoint.getSignature());
String methodName = methodSignature.getName();
String className = methodSignature.getDeclaringTypeName();
Object[] args = joinPoint.getArgs();

    String argwStr = JSON.toJSONString(args);

当使用切面时,如果使用args中包含了,request对象会到导致程序抛出throwable异常信息,所以加切面时建议将args数组中的内容进行移除。或者不要直接将前台的请求进行拦截后进行json转换。

解决方法:

 public Object businessLog(ProceedingJoinPoint joinPoint) throws Throwable {
    MethodSignature methodSignature = ((MethodSignature) joinPoint.getSignature());
    String methodName = methodSignature.getName();
    String className = methodSignature.getDeclaringTypeName();
    Object[] args = joinPoint.getArgs();
    //序列化时过滤掉request和response
    List<Object> logArgs = StreamUtil.streamOf(args)
            .filter(arg -> (!(arg instanceof HttpServletRequest) && !(arg instanceof HttpServletResponse)))
            .collect(Collectors.toList());
    String argStr = JSON.toJSONString(logArgs);
    Object result;

    try {
        result = joinPoint.proceed();
        String resultStr = JSON.toJSONString(result);
       //对入参出参做操作
    } catch (Exception e) {
      
        StackTraceElement[] stackTraceArray = e.getStackTrace();
        byte[] bytesArray = new byte[]{};
        for (int i = 0; i < stackTraceArray.length; i++) {
            byte[] bytes = stackTraceArray[i].toString().getBytes();
            bytesArray = Bytes.concat(bytesArray, bytes);
        }
        bytesArray = Bytes.concat(("*exception*" + e.getMessage() + "*exception*").getBytes(), bytesArray);
        //对异常信息做操作
        throw e;
    }
    return result;
}

   
    public static <T> Stream<T> streamOf(T[] array) {
        return ArrayUtils.isEmpty(array) ? Stream.empty() : Arrays.asList(array).stream();
    }

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值