Commit d46badee authored by 黎博's avatar 黎博

add Request Aspect

parent c25f15a3
......@@ -29,80 +29,80 @@ import java.util.Map;
/**
* 日志切面
*/
//@Aspect
//@Component
@Aspect
@Component
public class LogAspect {
// private Logger logger = LoggerFactory.getLogger(LogAspect.class);
// private Map<String, Object> logMap = new HashMap<>();
//
// @Pointcut("execution(public * cn.quantgroup.qaplatform.controller..*.*(..))")
// public void apiLog() {}
//
// /**
// * 前置通知:在连接点之前执行的通知
// *
// * @param joinPoint
// * @throws Throwable
// */
// @Before("apiLog()")
// public void doBefore(JoinPoint joinPoint) throws JsonProcessingException {
// ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
// HttpServletRequest request = servletRequestAttributes.getRequest();
// Signature signature = joinPoint.getSignature();
// MethodSignature methodSignature = (MethodSignature) signature;
// Method method = methodSignature.getMethod();
// logMap.put("url", request.getRequestURL().toString());
// logMap.put("method", request.getMethod());
// logMap.put("host", request.getRemoteAddr());
// logMap.put("class", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
// logMap.put("requestParams", getParameter(method, joinPoint.getArgs()));
// // 记录下请求内容
// logger.info("请求信息:" + JsonTransUtils.mapToJson(logMap));
// }
//
// /**
// * 后置通知:在连接点之后执行的通知
// *
// * @param ret
// * @throws Throwable
// */
// @AfterReturning(returning = "ret", pointcut = "apiLog()")
// public void doAfterReturning(Object ret) throws Throwable {
// // 处理完请求,返回内容
// logger.info("返回结果:" + JsonTransUtils.objToJson(ret));
// }
//
// /**
// * 根据方法和传入参数获取请求参数
// */
// private Object getParameter(Method method, Object[] args) {
// List<Object> argList = new ArrayList<>();
// Parameter[] parameters = method.getParameters();
// for(int i=0; i < parameters.length; i++) {
// //将RequestBody注解修饰的参数作为请求参数
// RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class);
// if (requestBody != null) {
// argList.add(args[i]);
// }
//
// //将RequestParam注解修饰的参数作为请求参数
// RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class);
// if (requestParam != null) {
// Map<String, Object> map = new HashMap<>();
// String key = parameters[i].getName();
// if (!StringUtils.isEmpty(requestParam.value())) {
// key = requestParam.value();
// }
// map.put(key, args[i]);
// argList.add(map);
// }
// }
// if (argList.size() == 0) {
// return null;
// } else if (argList.size() == 1) {
// return argList.get(0);
// } else {
// return argList;
// }
// }
private Logger logger = LoggerFactory.getLogger(LogAspect.class);
private Map<String, Object> logMap = new HashMap<>();
@Pointcut("execution(public * cn.quantgroup.qaplatform.controller..*.*(..))")
public void apiLog() {}
/**
* 前置通知:在连接点之前执行的通知
*
* @param joinPoint
* @throws Throwable
*/
@Before("apiLog()")
public void doBefore(JoinPoint joinPoint) throws JsonProcessingException {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = servletRequestAttributes.getRequest();
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
Method method = methodSignature.getMethod();
logMap.put("url", request.getRequestURL().toString());
logMap.put("method", request.getMethod());
logMap.put("host", request.getRemoteAddr());
logMap.put("class", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
logMap.put("requestParams", getParameter(method, joinPoint.getArgs()));
// 记录下请求内容
logger.info("请求信息:" + JsonTransUtils.mapToJson(logMap));
}
/**
* 后置通知:在连接点之后执行的通知
*
* @param ret
* @throws Throwable
*/
@AfterReturning(returning = "ret", pointcut = "apiLog()")
public void doAfterReturning(Object ret) throws Throwable {
// 处理完请求,返回内容
logger.info("返回结果:" + JsonTransUtils.objToJson(ret));
}
/**
* 根据方法和传入参数获取请求参数
*/
private Object getParameter(Method method, Object[] args) {
List<Object> argList = new ArrayList<>();
Parameter[] parameters = method.getParameters();
for(int i=0; i < parameters.length; i++) {
//将RequestBody注解修饰的参数作为请求参数
RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class);
if (requestBody != null) {
argList.add(args[i]);
}
//将RequestParam注解修饰的参数作为请求参数
RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class);
if (requestParam != null) {
Map<String, Object> map = new HashMap<>();
String key = parameters[i].getName();
if (!StringUtils.isEmpty(requestParam.value())) {
key = requestParam.value();
}
map.put(key, args[i]);
argList.add(map);
}
}
if (argList.size() == 0) {
return null;
} else if (argList.size() == 1) {
return argList.get(0);
} else {
return argList;
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment