Springboot项目获取所有接口
@Autowired private WebApplicationContext applicationContext; @Override public List getAllUrl() { RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class); // 获取url与类和方法的对应信息 Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods(); List<Map<String, String>> list = new ArrayList<Map<String, String>>(); for (Map.Entry<RequestMappingInfo, HandlerMethod> m : map.entrySet()) { Map<String, String> map1 = new HashMap<String, String>(); RequestMappingInfo info = m.getKey(); HandlerMethod method = m.getValue(); //获取当前方法所在类名 Class<?> bean = method.getBeanType(); //使用反射获取当前类注解内容 Api api = bean.getAnnotation(Api.class); RequestMapping requestMapping = bean.getAnnotation(RequestMapping.class); String[] value = requestMapping.value(); map1.put("parent",value[0]) //获取方法上注解以及注解值 ApiOperation methodAnnotation = method.getMethodAnnotation(ApiOperation.class); String privilegeName = methodAnnotation.notes(); 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; }
获取项目下所有http接口的信息
一、接口信息类
新建一个类用于存放http接口的相关信息
class RequestToMethodItem { public String controllerName; public String methodName; public String requestType; public String requestUrl; public Class<?>[] methodParmaTypes; public String getControllerName() { return controllerName; } public void setControllerName(String controllerName) { this.controllerName = controllerName; } public String getMethodName() { return methodName; } public void setMethodName(String methodName) { this.methodName = methodName; } public String getRequestType() { return requestType; } public void setRequestType(String requestType) { this.requestType = requestType; } public String getRequestUrl() { return requestUrl; } public void setRequestUrl(String requestUrl) { this.requestUrl = requestUrl; } public Class<?>[] getMethodParmaTypes() { return<strong style="color:transparent">来2源gaodaima#com搞(代@码&网</strong> methodParmaTypes; } public void setMethodParmaTypes(Class<?>[] methodParmaTypes) { this.methodParmaTypes = methodParmaTypes; } public RequestToMethodItem(String requestUrl, String requestType, String controllerName, String requestMethodName, Class<?>[] methodParmaTypes){ this.requestUrl = requestUrl; this.requestType = requestType; this.controllerName = controllerName; this.methodName = requestMethodName; this.methodParmaTypes = methodParmaTypes; } }