获取项目中所有URL--获取swagger上展示的接口信息 - 劈天造陆 - 博客园

获取项目中所有URL--获取swagger上展示的接口信息

有时我们需要接口的一些基本信息,比如接口请求路径,接口请求方式等,我们用这些信息来做判断,或者入库。

我在开发接口权限的时候就遇到了这个问题,之前写的接口很多,现在需要将这些接口信息存到数据库中,

用来做接口的权限操作,经过一番查阅,在此汇总了一下:

复制代码; "复制代码")

@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);
}

复制代码; "复制代码")

劈天造陆,开辟属于自己的天地!!!与君共勉

分类: spring boot, 实用技巧

好文要顶;) 关注我;) 收藏该文;) ; "分享至新浪微博") ; "分享至微信")

劈天造陆
关注 - 6
粉丝 - 65

+加关注;)

1

0

« 上一篇: 缓存穿透/击穿/雪崩/降级
» 下一篇: Redis数据结构和使用场景,redis内存淘汰策略

posted @ 2019-05-09 15:02  劈天造陆  阅读(7754)  评论(0)  编辑  收藏举报)


原网址: 访问
创建于: 2021-10-29 11:20:29
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论