切面报错写法:
/*
@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时无法进行序列号,因而报错
MultipartFile 类型也无法转换为JSON
【解决方式】
/*
@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--------");
}
}