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

切面报错写法:

/*
@auther aa

@create_date 2019-12-24 15:15

@Desc 接口请求日志切面

**/

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class WebLogAcpect {
    protected static final Logger logger = LoggerFactory.getLogger(WebLogAcpect.class);

    /**
     * 定义切入点
     * */
    @Pointcut("execution(* com.smcv.xyx.partOrder.manage.controller.view..*.*(..))")
    public void webLog(){}

    /**
     * 前置通知
     * */
    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint)throws Exception{
        //接收到请求,打印请求内容
        ServletRequestAttributes attributes =(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //记录请求内容
        logger.info("-------------log print start--------");
        logger.info("请求接口名:{},请求方式:{}",request.getRequestURI().toString(),request.getMethod());
//报错位置在此下方
//报错位置在此下方
//报错位置在此下方
        logger.info("请求参数:{}",JSON.toJSONString(joinPoint.getArgs()));

    }

    @AfterReturning(returning="ret",pointcut = "webLog()")
    public void doAfterReturning(Object ret)throws Exception{
        ServletRequestAttributes attributes =(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //接收到响应,打印请求内容
        logger.info("请求接口:{}的响应为:{}",request.getRequestURI(),JSON.toJSONString(ret));
        logger.info("-------------log print end--------");

    }

}

因为:当请求中带有ServletRequest或ServletResponse时无法进行序列号,因而报错

【解决方式】

/*
@auther aa

@create_date 2019-12-24 15:15

@Desc 接口请求日志切面

**/

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class WebLogAcpect {
    protected static final Logger logger = LoggerFactory.getLogger(WebLogAcpect.class);

    /**
     * 定义切入点
     * */
    @Pointcut("execution(* com.smcv.xyx.partOrder.manage.controller.view..*.*(..))")
    public void webLog(){}

    /**
     * 前置通知
     * */
    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint)  {
        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();
            }
        }
        logger.info("请求接口名:{}", joinPoint.getSignature().getName());
        logger.info("请求参数:{}", paramter);
    }

    @AfterReturning(returning="ret",pointcut = "webLog()")
    public void doAfterReturning(Object ret)throws Exception{
        ServletRequestAttributes attributes =(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //接收到响应,打印请求内容
        logger.info("请求接口:{}的响应为:{}",request.getRequestURI(),JSON.toJSONString(ret));
        logger.info("-------------log print end--------");

    }

}

 

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值