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

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


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 org.springframework.util.StreamUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;



@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) {
        if (log.isDebugEnabled()) {
            log.debug("开始执行方法[around],参数[{}]", JSON.toJSONString(pjp.getArgs()));
        }

        Object[] args = pjp.getArgs();
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        String[] parameterNames = methodSignature.getParameterNames();
        Map map = new HashMap<>();
        for (int i = 0; i < args.length; i++) {
            String paramName = parameterNames[i];
            Object paramValue = args[i];
            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));
        logInterface.setParticipation(JsonUtils.toJSONString(map));//入参
        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, "执行接口异常");
        }
    }

}

主要是这一段:

        Object[] args = pjp.getArgs();
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        String[] parameterNames = methodSignature.getParameterNames();
        Map map = new HashMap<>();
        for (int i = 0; i < args.length; i++) {
            String paramName = parameterNames[i];
            Object paramValue = args[i];
            map.put(paramName,paramValue);
        }

效果非常好!

你可能感兴趣的:(杂说,hive,hadoop,数据仓库)