@RequestMapping(value = "/v1/getAllUrl", method = RequestMethod.POST)
public Object getAllUrl() {
RequestMappingHandlerMapping mapping = SpringContextHolder.getBean(RequestMappingHandlerMapping.class);
// 获取url与类和方法的对应信息
Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods(); List<String> urlList = new ArrayList<>();
for (RequestMappingInfo info : map.keySet()) {
// 获取url的Set集合,一个方法可能对应多个url
Set<String> patterns = info.getPatternsCondition().getPatterns(); for (String url : patterns) {
urlList.add(url);
}
} List<Map<String, String>> list = new ArrayList<>();
for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) {
Map<String, String> map1 = new HashMap<>();
RequestMappingInfo info = m.getKey();
HandlerMethod method = m.getValue();
PatternsRequestCondition p = info.getPatternsCondition();
for (String url : p.getPatterns()) {
map1.put("url", url);
}
map1.put("className", method.getMethod().getDeclaringClass().getName()); // 类名
map1.put("method", method.getMethod().getName()); // 方法名
RequestMethodsRequestCondition methodsCondition = info.getMethodsCondition();
for (RequestMethod requestMethod : methodsCondition.getMethods()) {
map1.put("type", requestMethod.toString());
}
list.add(map1);
} return list;
} private String getMapper() {
String result = "";
RequestMappingHandlerMapping rmhp = SpringContextHolder.getBean(RequestMappingHandlerMapping.class);
Map<RequestMappingInfo, HandlerMethod> map = rmhp.getHandlerMethods();
for (RequestMappingInfo info : map.keySet()){
result += info.getPatternsCondition().toString();
HandlerMethod hm = map.get(info);
result += hm.getBeanType().getName();
result += getMethodParams(hm.getBeanType().getName(),hm.getMethod().getName());
result += info.getProducesCondition();
}
return result;
} private String getMethodParams(String className,String methodName){
String result="";
try{
ClassPool pool=ClassPool.getDefault();
ClassClassPath classPath = new ClassClassPath(this.getClass());
pool.insertClassPath(classPath);
CtMethod cm =pool.getMethod(className, methodName);
// 使用javaassist的反射方法获取方法的参数名
MethodInfo methodInfo = cm.getMethodInfo();
CodeAttribute codeAttribute = methodInfo.getCodeAttribute();
LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);
result=cm.getName() + "(";
if (attr == null) {
return result + ")";
}
CtClass[] pTypes=cm.getParameterTypes();
String[] paramNames = new String[pTypes.length];
int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;
for (int i = 0; i < paramNames.length; i++) {
if(!pTypes[i].getSimpleName().startsWith("HttpServletRe")){
result += pTypes[i].getSimpleName();
paramNames[i] = attr.variableName(i + pos);
result += " " + paramNames[i]+",";
}
}
if(result.endsWith(",")){
result=result.substring(0, result.length()-1);
}
result+=")";
}catch(Exception e){
e.printStackTrace();
}
return result;
}
04-17 15:13