获取项目中所有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包 第二步,根 ...
随机推荐
- Research Guide for Video Frame Interpolation with Deep Learning
Research Guide for Video Frame Interpolation with Deep Learning This blog is from: https://heartbeat ...
- linux学习(1):linux命令大全
Linux命令 目录 1 文件管理... 5 1.1 basename. 5 1.2 cat 5 1.3 cd. 5 1.4 ...
- 从安装PHP到第一个tomcat执行的hello world其实没那么难
001 初入门的朋友问我为什么她的PHP老是不能安装运行成功,作为一个乐(shi)于(li)助(liao)人(mei)的半程序员, 自然是要好好研究然后手把手教妹纸了! 002 话不多说,进入正题 为 ...
- Spring MVC -- 表达式于语言(EL)
JSP 2.0最重要的特性之一就是表达式语言(EL),JSP用户可以用它来访问应用程序数据.由于受到ECMAScript和XPath表达式语言的启发,EL也设计成可以轻松地编写免脚本(就是不用在jsp ...
- Centos7.3部署安装Maven
需要提前配置好java环境 1.下载压缩包 将安装包下载到root家目录下 http://maven.apache.org/download.cgi 选择bin.tar.gz格式的压缩包 我是直接执行 ...
- 2017ACM/ICPC广西邀请赛 1004 Covering
Covering Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- LeetCode 84. 柱状图中最大的矩形(Largest Rectangle in Histogram)
84. 柱状图中最大的矩形 84. Largest Rectangle in Histogram
- nginx实现tcp负载均衡
1 安装支持库 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel yum install pcre-d ...
- [Oracle] - 查看数据库中每个表占用空间大小,及进行表压缩
查询用户创建的表 select * from user_tab_comments; -- 查询本用户的表,视图等. select * from user_col_comments; -- 查询本用户的 ...
- 【C++札记】多态
C++中多态是面向对象设计思想的重要特性,同名具有不同功能函数,该函数调用过程执行不同的功能.多态的原理是通过一张虚函数表(Virtual Table)实现的.动多态会牺牲一些空间和效率来最终实现动态 ...