切面的时候获取POST和GET参数的key和value

package cn.net.cdsz.ccb.common.aspect;


import club.newepoch.utils.JsonUtils;
import cn.net.cdsz.ccb.business.model.pojo.LogInterface;
import cn.net.cdsz.ccb.common.event.GenTables;
import cn.net.cdsz.ccb.common.exception.SystemException;
import cn.net.cdsz.ccb.common.utils.ToolUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;



@Slf4j
@Aspect
@Order(value = 11)
@Component
public class MockAspect {
    @Autowired
    private GenTables genTables;
    @Autowired
    protected HttpServletRequest request;
    @Pointcut("execution(* cn.net.cdsz.ccb..*.controller..*(..))")
    public void mockController() {
        //define pointcut
    }

    @SuppressWarnings("cdsz-sonar-rules:SZLEXCEPTION")
    @Around("mockController()")
    public Object around(ProceedingJoinPoint pjp) {
        Object[] args = pjp.getArgs();
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        String[] parameterNames = methodSignature.getParameterNames();
        Map<String,Object> map = new ConcurrentHashMap<>();
        for (int i = 0; i < args.length; i++) {
            String paramName  = parameterNames[i];
            Object paramValue = args[i];
            if((paramValue instanceof HttpServletRequest) || (paramValue instanceof HttpServletResponse)){
                continue;
            }
            map.put(paramName,paramValue);
        }
        long startTim = System.currentTimeMillis();
        LogInterface logInterface = new LogInterface();
        logInterface.setKeyId(Long.valueOf(ToolUtils.getKeyId()).longValue());
        logInterface.setHost(request.getRemoteHost());
        logInterface.setMethod(request.getMethod());
        logInterface.setLoginAddress(request.getQueryString());
        logInterface.setStackTrace("");
        logInterface.setTimeBegin(new Timestamp(startTim));
        String participation="";
        try {
            participation =  JsonUtils.toJSONString(map);
        }catch (Exception e){
            participation="Servlet的异步操作!";
        }
        logInterface.setParticipation(participation);//入参
        logInterface.setUri(request.getRequestURI());
        try {
            Object result = pjp.proceed();
            long endTim = System.currentTimeMillis();
            long elapsedTim = endTim - startTim;
            logInterface.setReturnValue(JsonUtils.toJSONString(result));//出参
            logInterface.setTimeEnd(new Timestamp(endTim));
            logInterface.setTimePeriod((elapsedTim));
            genTables.save(logInterface);
            if (log.isDebugEnabled()) {
                log.debug("方法[around]执行完成,返回值[{}]", JSON.toJSONString(result));
            }
            return result;
        } catch (Throwable throwable) {
            long endTim = System.currentTimeMillis();
            long elapsedTim = endTim - startTim;
            logInterface.setReturnValue("");//出参
            logInterface.setStackTrace(JsonUtils.toJSONString(throwable.fillInStackTrace()));
            logInterface.setTimeEnd(new Timestamp(endTim));
            logInterface.setTimePeriod((elapsedTim));
            genTables.save(logInterface);
            throw new SystemException(throwable, "执行接口异常");
        }
    }

}

主要是这一段:

        Map<String,Object> map = new ConcurrentHashMap<>();
        for (int i = 0; i < args.length; i++) {
            String paramName  = parameterNames[i];
            Object paramValue = args[i];
            if((paramValue instanceof HttpServletRequest) || (paramValue instanceof HttpServletResponse)){
                continue;
            }
            map.put(paramName,paramValue);
        }

效果非常好!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值