java.lang.IllegalStateException: It is illegal to call this method if the current request is not ...

AOP的日志拦截类中,抛出异常:

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

主要原因:对方法的参数使用JSON.toJSONString(args[index])转换时,有异常抛出【如果参数类型是请求和响应的http,使用JSON.toJSONString()转换会抛异常】

问题分许:FastJson是根据bean的get/set方法做反射解析的,当遇到Servlet3规范时,恰好存在一个getAsyncContext()方法,内部的判断逻辑是servlet异步模式为false,直接抛出IllegalStateException,导致此种尴尬的异常出现,进而阻塞了工作的进程。

// reuquest  源码
  public AsyncContext getAsyncContext() {
        if (!this.isAsyncStarted()) {
            throw new IllegalStateException(sm.getString("request.notAsync"));
        } else {
            return this.asyncContext;
        }
    }

解决方案:将不能进行序列化的入参过滤掉,只要留下我们需要记录的入参参数记录到日志中即可

        //请求参数
            Object[] args = point.getArgs();
            List argsList=new ArrayList<>();
            for (int i = 0; i < args.length; i++) {
                // 如果参数类型是请求和响应的http,则不需要拼接【这两个参数,使用JSON.toJSONString()转换会抛异常】
                if (args[i] instanceof HttpServletRequest || args[i] instanceof HttpServletResponse)
                {
                    continue;
                }
                argsList.add(args[i]);
            }
 

                            
                        
                    
                    
                    

你可能感兴趣的:(java.lang.IllegalStateException: It is illegal to call this method if the current request is not ...)