HttpServletRequest 如何获取Method 和 ProceedingJoinPoint的getArgs
看到一个 aop方法获取请求的参数
public void save(String username, String browser, String ip, ProceedingJoinPoint joinPoint, Log log) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
com.icss.annotation.Log aopLog = method.getAnnotation(com.icss.annotation.Log.class);
// 方法路径
String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()";
// 描述
if (log != null) {
log.setDescription(aopLog.value());
}
assert log != null;
log.setRequestIp(ip);
try {
log.setAddress(StringUtils.getCityInfo(log.getRequestIp()));
} catch (Exception e) {
e.printStackTrace();
}
log.setMethod(methodName);
log.setUsername(username);
log.setParams(StringUtils.getParameter(method, joinPoint.getArgs()));
log.setBrowser(browser);
logRepository.save(log);
}
获取请求参数方法
/**
* 根据方法和传入的参数获取请求参数
*/
public static String 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 "";
}
return argList.size() == 1 ? JSONUtil.toJsonStr(argList.get(0)) : JSONUtil.toJsonStr(argList);
}
现在想写一个工具类 传入HttpServletRequest request 获取到请求参数
但是问题是怎么获取到(Method method, Object[] args) 这两个参数呢