zoukankan      html  css  js  c++  java
  • java中获取swagger所有的接口,url,请求方式等信息

    方法

    @Autowired
        WebApplicationContext applicationContext;
    
        @RequestMapping(value = "/getAllURL", method = RequestMethod.POST)
        public Object getAllURL() {
            List<Map<String, String>> resultList = new ArrayList<>();
    
            RequestMappingHandlerMapping requestMappingHandlerMapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
            // 获取url与类和方法的对应信息
            Map<RequestMappingInfo, HandlerMethod> map = requestMappingHandlerMapping.getHandlerMethods();
    
            for (Map.Entry<RequestMappingInfo, HandlerMethod> mappingInfoHandlerMethodEntry : map.entrySet()) {
                Map<String, String> resultMap = new LinkedHashMap<>();
    
                RequestMappingInfo requestMappingInfo = mappingInfoHandlerMethodEntry.getKey();
                HandlerMethod handlerMethod = mappingInfoHandlerMethodEntry.getValue();
    
                resultMap.put("className",handlerMethod.getMethod().getDeclaringClass().getName()); // 类名
                Annotation[] parentAnnotations = handlerMethod.getBeanType().getAnnotations();
                for (Annotation annotation : parentAnnotations) {
                    if (annotation instanceof Api) {
                        Api api = (Api) annotation;
                        resultMap.put("classDesc",api.value());
                    } else if (annotation instanceof RequestMapping) {
                        RequestMapping requestMapping = (RequestMapping) annotation;
                        if (null != requestMapping.value() && requestMapping.value().length > 0) {
                            resultMap.put("classURL",requestMapping.value()[0]);//类URL
                        }
                    }
                }
                resultMap.put("methodName", handlerMethod.getMethod().getName()); // 方法名
                Annotation[] annotations = handlerMethod.getMethod().getDeclaredAnnotations();
                if (annotations != null) {
                    // 处理具体的方法信息
                    for (Annotation annotation : annotations) {
                        if (annotation instanceof ApiOperation) {
                            ApiOperation methodDesc = (ApiOperation) annotation;
                            String desc = methodDesc.value();
                            resultMap.put("methodDesc",desc);//接口描述
                        }
                    }
                }
                PatternsRequestCondition p = requestMappingInfo.getPatternsCondition();
                for (String url : p.getPatterns()) {
                    resultMap.put("methodURL",url);//请求URL
                }
                RequestMethodsRequestCondition methodsCondition = requestMappingInfo.getMethodsCondition();
                for (RequestMethod requestMethod : methodsCondition.getMethods()) {
                    resultMap.put("requestType",requestMethod.toString());//请求方式:POST/PUT/GET/DELETE
                }
                resultList.add(resultMap);
            }
            return JSONArray.fromObject(resultList);
        }

    返回结果

     转载自:https://www.cnblogs.com/java-spring/p/10838514.html

  • 相关阅读:
    GCD实现多个定时器,完美避过NSTimer的三大缺陷(RunLoop、Thread、Leaks)
    iOS适配UIViewView/WKWebView,H5生成长图,仿微信进度条
    翻译jquery官方的插件制作方法
    javascript引用和赋值
    薯片公司真实JS面试题(乐视TV)
    caller、call、apply、callee的用法和意思
    常用javascript类型判断
    Git 常用命令笔记(不定期持续记录)
    sublime text2 emmet 安装
    hash"#"
  • 原文地址:https://www.cnblogs.com/Crush123/p/14522123.html
Copyright © 2011-2022 走看看