zoukankan      html  css  js  c++  java
  • 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();
    }

    Object[] args = joinPoint.getArgs();
    Object[] arguments = new Object[args.length];
    for (int i = 0; i < args.length; i++) {
    if (args[i] instanceof ServletRequest || args[i] instanceof ServletResponse || args[i] instanceof MultipartFile) {
    //ServletRequest不能序列化,从入参里排除,否则报异常:java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
    //ServletResponse不能序列化 从入参里排除,否则报异常:java.lang.IllegalStateException: getOutputStream() has already been called for this response
    continue;
    }
    arguments[i] = args[i];
    }
    String paramter = "";
    if (arguments != null) {
    try {
    paramter = JSONObject.toJSONString(arguments);
    } catch (Exception e) {
    paramter = arguments.toString();
    }
    }

  • 相关阅读:
    VIM快捷键(转)
    VIM中文乱码
    vsftpd.conf 联机手册
    keepalived nginx 主备配置
    Keepalived 主备配置
    Linux centos7 安装 keepalived-2.0.6
    Linux centos开机执行JAR Shell脚本
    Nginx负载均衡案例
    Windows虚拟机安装Linux系统
    Linux centos7 redis安装教程
  • 原文地址:https://www.cnblogs.com/lovelyp/p/10255440.html
Copyright © 2011-2022 走看看