美文网首页工作生活
java.lang.IllegalStateException:

java.lang.IllegalStateException:

作者: 逆水寻洲 | 来源:发表于2019-07-01 14:26 被阅读0次

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<Object> 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:

    本文链接:https://www.haomeiwen.com/subject/kjfgcctx.html