/**
* @Description 获取@RequestBody ,@RequestParam注解的参数
* @param method HttpServletRequest request = HttpContextUtil.getHttpServletRequest();
* MethodSignature signature = (MethodSignature) joinPoint.getSignature();
* Method method = signature.getMethod();
* Object[] args = joinPoint.getArgs();
* @param args
* @Throws
* @Return java.lang.String
* @Date 2024-03-08 17:00:20
* @Author WangKun
*/
public static String getParameter(HttpServletRequest request ,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);
RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class);
if (requestBody != null) {
argList.add(args[i]);
} else if (requestParam != null) {
//将RequestParam注解修饰的参数作为请求参数
Map<String, Object> map = new HashMap<>(8);
String key;
if (!StringUtils.isEmpty(requestParam.value()) && !Objects.isNull(args[i])) {
key = requestParam.value();
if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(args[i].toString())) {
if ("password".equals(key)){
map.put(key, "***");
}else{
map.put(key, args[i]);
}
argList.add(map);
}
}
}
parameters[i].getType().getClass();
}
if (argList.isEmpty()) {
//当请求参数中没有使用@RequestParam or @RequestBody ,使用request.getParameterMap()获取请求参数
if (parameters.length > 0) {
Map<String, String[]> parameterMap = request.getParameterMap();
Map<String, Object> map = new HashMap<>(8);
parameterMap.forEach((k, v) -> {
List<String> strings = Arrays.asList(v);
if (!StringUtils.isBlank(k) && !strings.isEmpty()) {
if ("password".equals(k)){
map.put(k, "***");
}else{
map.put(k, strings.get(0));
}
}
});
if (!map.isEmpty()) {
argList.add(map);
}
}
if (argList.isEmpty()) {
return null;
}
}
return argList.size() == 1 ? JSON.toJSONString(argList.get(0)) : JSON.toJSONString(argList);
}
01-26
8892
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-13
7850
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-25
2万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-01
1798
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交