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

本文探讨了在使用AOP切面编程时遇到的非法状态异常问题,详细解析了异常产生的原因,提供了有效的解决方案,包括如何正确处理request对象,避免在序列化参数时引发异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
    }

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值