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);
}
效果非常好!