AOP中获取get请求的key和value值

发布时间 2023-03-24 17:22:18作者: 迷糊桃

 

    public void afterReturning(JoinPoint joinPoint) {
        // 下面两个数组中,参数值和参数名的个数和位置是一一对应的。
        Object[] args = joinPoint.getArgs(); // 参数值
        String[] argNames = ((MethodSignature)joinPoint.getSignature()).getParameterNames(); // 参数名
        String name = joinPoint.getTarget().getClass().getName();// 请求地址
        String name1 = joinPoint.getSignature().getName();// 请求类方法
}
joinPoint.getArgs();这个方法也可以获取到post请求。
joinPoint.getArgs()返回的数组中携带有Request(HttpServletRequest)或者Response(HttpServletResponse)对象,导致序列化异常。不要直接将前台的请求进行拦截后进行json转换
//log.info("Request Args  : {}", JSON.toJSONString(joinPoint.getArgs())); 序列化异常
Object[] args = joinPoint.getArgs();
if (ArrayUtils.isNotEmpty(args)) {
    List<Object> logArgs = Arrays.stream(args).filter(arg -> (!(arg instanceof HttpServletRequest) && !(arg instanceof HttpServletResponse)))
            .collect(Collectors.toList());
    log.info("Request Args  : {}", JSON.toJSONString(logArgs));
} else {
    log.info("Request Args  : null");
}

还需要注意,joinPoint.getArgs()获取到的是一个list数组,json请求需要JSON.toJSONString(logArgs)).get(0).toString()

参考https://blog.csdn.net/Ying_ph/article/details/119636866大佬的代码

package com.shinkeer.core.config.aop;
 
import com.shinkeer.core.utils.JsonUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
 
import java.lang.reflect.Method;
 
/**
 * @description: 日志切面打印
 **/
@Aspect
@Component
public class LogAop {
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    @Pointcut("@annotation(xxx.xxx.SysLog)")
    private void logPointCut() {
    }
 
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        MethodSignature ms = (MethodSignature) point.getSignature();
        Method method = ms.getMethod();
        logger.info("===============请求内容===============");
        logger.info("请求地址:" + point.getTarget().getClass().getName());
        logger.info("请求方式:" + method);
        logger.info("请求类方法:" + point.getSignature().getName());
        logger.info("请求类方法参数:" + JsonUtils.getJsonString(point.getArgs()));
        logger.info("===============请求内容===============");
        long beginTime = System.currentTimeMillis();
        //执行方法
        Object result = point.proceed();
        //执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;
        logger.info("===============返回内容===============");
        String jsonString = JsonUtils.getJsonString(result);
        if (jsonString.length() > 10000) {
            logger.info("Response内容:" + "返回内容过大");
        } else {
            logger.info("Response内容:" + jsonString);
        }
        logger.info("请求响应时间:" + time + "ms");
        logger.info("===============返回内容===============");
        return result;
    }
}