swagger生成的页面api接口统计,有几种方法
- 直接在前端用js提取出来,较麻烦(不推荐,不同版本的页面生成的标签有可能不一样,因此可能提取不出来)
//api
let a = document.getElementsByClassName("opblock-summary-path");
var temp = "";
for(var i=0;i<a.length;i++){
temp += "
" + a[i].getElementsByTagName("span")[0].innerText}
//请求方法
let b = document.getElementsByClassName("opblock-summary-method");
var temp = "";
for(var i=0;i<b.length;i++){
temp += "
" + b[i].innerText}
//描述
let c = document.getElementsByClassName("opblock-summary-description");
var temp = "";
for(var i=0;i<c.length;i++){
temp += "
" + c[i].innerText}
- 用页面自带的api-doc中的json数据,解析数据后直接生成csv表格,需要编程
https://github.com/ghdefe/swagger-json-to-csv
public class JsonToTxtApplication { public static void main(String[] args) throws IOException { SpringApplication.run(JsonToTxtApplication.class, args); FileInputStream in = new FileInputStream("1.txt"); JsonNode jsonNode = new ObjectMapper().readTree(in); /** * 取所有数据并存到HashMap中 */ String api; HashMap<String, List<Root>> hm = new HashMap<>(); JsonNode node = jsonNode.findValue("paths"); Iterator<String> stringIterator = node.fieldNames(); while (stringIterator.hasNext()) { JsonNode tags = node.findValue((api = stringIterator.next())); //api Iterator<String> methodsname = tags.fieldNames(); while (methodsname.hasNext()) { String method = methodsname.next(); //方法 JsonNode methods = tags.findValue(method); String name = methods.findValue("tags").get(0).asText(); String description = methods.findValue("description").asText(); Root root = new Root(name, method, api,description); //当前查询到的一个接口数据 //放到hashmap里管理 if (hm.containsKey(root.getName())) { List<Root> roots = hm.get(root.getName()); roots.add(root); hm.put(root.getName(), roots); } else { ArrayList<Root> roots = new ArrayList<>(); roots.add(root); hm.put(root.getName(), roots); } } } /** * 获得name的顺序,并按顺序写入csv */ File file = new File("result.csv"); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file), "GBK")); //excel不能读取utf-8编码的csv文件 Iterator<JsonNode> names = jsonNode.findValue("tags").iterator(); while (names.hasNext()) { String name = names.next().findValue("name").asText(); Iterator<Root> iterator1 = hm.get(name).iterator(); bufferedWriter.write(name + ","); Boolean isFirst = true; while (iterator1.hasNext()) {
//如果是第一行增加name,如果不是填入空白格 if (!isFirst) { bufferedWriter.write(","); } else { isFirst = false; } Root next = iterator1.next(); bufferedWriter.write(next.getMethod() + "," + next.getApi() + "," + next.getDescription()); bufferedWriter.newLine(); } } bufferedWriter.close();
//打开生成的csv文件 Runtime.getRuntime().exec("cmd /c start F:/Project/JsonSoup/result.csv"); System.out.println("done"); } }
- 还可以利用xpath helper提取,需要学习一下xpath语法,还需要安装浏览器插件xpath helper。一些环境要求不能安装软件时该方法不适用。其实这种方法跟js提取是一样的,区别就在于xpath提取更加简便快捷。