一、登录swagger网页端,F12,会有一个接口,拿他的返回值生成一个本地文档
二、执行以下代码
FileInputStream in = new FileInputStream("C:\\Users\\azhan\\Downloads\\temp.java");
JsonNode jsonNode = new ObjectMapper().readTree(in);
/**
* 取所有数据并存到HashMap中
*/
String api;
Map<String, List<Root>> hm = new HashMap<>();
JsonNode node = jsonNode.findValue("paths");
Iterator<String> stringIterator = node.fieldNames();
while (stringIterator.hasNext()) {
//api
JsonNode tags = node.findValue((api = stringIterator.next()));
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("summary").asText();
//当前查询到的一个接口数据放到hashmap里管理
Root root = new Root(name, method, api,description);
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);
}
}
// 处理相同数据
for (String s : hm.keySet()) {
final List<Root> roots = hm.get(s);
final List<Root> collect = roots.stream().distinct().collect(Collectors.toList());
hm.put(s,collect);
}
}
/**
* 获得name的顺序,并按顺序写入csv
*/
File file = new File("C:\\Users\\azhan\\Downloads\\result.csv");
//excel不能读取utf-8编码的csv文件
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file), "GBK"));
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;
//如果是第一行增加name,如果不是填入空白格
while (iterator1.hasNext()) {
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 C:\\Users\\azhan\\Downloads\\result.csv");
System.out.println("done");
根据需要自行更改本地文件的地址和下载文件的地址!
需要什么参数,可以根据实际情况修改代码!