获取项目中所有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);
}
获取项目中所有URL--获取swagger上展示的接口信息的更多相关文章
- Django自动获取项目中的全部URL
import re from collections import OrderedDict from django.conf import settings from django.utils.mod ...
- ASP.NET 获取来源网站的网址,获取上一网页的网址,获取来源网页的URL,获取上一网页的URL
ASP.NET 获取来源网站的网址,获取上一网页的网址,获取来源网页的URL, 获取上一网页的URL Uri Url = HttpContext.Current.Request.UrlReferrer ...
- JS 获取字符串中的url并返回其下标索引
//获取字符串中的url极其下标索引 function getHttpUrlArray(s) { var s1 = s.match(/http.*/); if(s1 == null) { return ...
- Java获取项目中的路径 分类: Java Game 2014-08-14 10:17 122人阅读 评论(0) 收藏
在项目中经常需要获取某个文件的路径: 在这里提供一些获取路径的方法.. 1.此种方式获取的路径,是当前类所在的路径: UserDAOTest.class.getResource("UserD ...
- [置顶]
django快速获取项目所有的URL
django快速获取项目所有的URL django1.10快速获取项目所有的URL列表,可以用于权限控制 函数如下: import re def get_url(urllist , parent='' ...
- django2自动发现项目中的url
根据路飞学城luffycity.com 的crm项目修改的 1 url入口:rbac/urls.py urlpatterns = [ ... # 批量操作权限 re_path(r'^multi/per ...
- 自动发现项目中的URL,django1版本和django2版本
一.django 1 版本 routers.py import re from collections import OrderedDict from django.conf import setti ...
- 自动发现项目中的url
def check_url_exclude(url): """ 判断url是否需要自动被发现,如果不是则移除 :param url: 自动发现的url :return: ...
- 项目中通过Sorlj获取索引库中的数据
在开发项目中通过使用Solr所提供的Solrj(java客户端)获取索引库中的数据,这才是真正对项目起实质性作用的功能,提升平台的检索性能及检索结果的精确性 第一步,引入相关依赖的jar包 第二步,根 ...
随机推荐
- axios 文件下载代码 片段
<script type="text/javascript"> axios({ method:'post', url: 'url', // 最好qs.stringify ...
- python pycharm错误集锦
url:http://www.cnblogs.com/hinimix/p/8016859.html 1, this list creation could be rewritten as a list ...
- mysql死锁(锁与事务)
线上某服务时不时报出如下异常(大约一天二十多次):“Deadlock found when trying to get lock;”. Oh, My God! 是死锁问题.尽管报错不多,对性能目前看来 ...
- jmeter测试 flask 接口请求
jmeter测试 flask 接口请求 flask的代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- from flask import Flas ...
- 工具系列 | VScode VS Live Share 实时编码分享(和你的小伙伴一起写代码吧)
Visual Studio Live Share能干啥? 分享任何语言,任何应用程序 无论您正在构建什么类型的应用程序,您正在编写什么语言,或者您的操作系统如何:在您需要协作时,Live Share会 ...
- Intellij idea 告警:URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)
URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs) 一.快捷键方式 鼠标移动到出错的的地方 ...
- python2中的unicode()函数在python3中会报错:
python2中的unicode()函数在python3中会报错:NameError: name 'unicode' is not defined There is no such name in P ...
- 初识HDFS(10分钟了解HDFS、NameNode和DataNode)
概览 首先我们来认识一下HDFS, HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.它其实是将一个大文件分成若干块保存在不同服务器的多个节点中.通 ...
- [LeetCode] 749. Contain Virus 包含病毒
A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. ...
- 当代码上传git时,提示Repository not found The requested repository does not exist, or you do not have permission to access it. fatal: Could not read from remote repository. Please make sure you have the corre
1.检查当前git中设置的用户名与邮箱是否与自己电脑上的一致. 看这个文件中 如果不一致,只需要把里面的内容全部复制出来添加到git(看下图位置) 这是再执行:git push -u origin m ...