Swagger导出word和excel文档 您所在的位置:网站首页 调查问卷怎么分析数据模板论文 Swagger导出word和excel文档

Swagger导出word和excel文档

2024-02-21 19:35| 来源: 网络整理| 查看: 265

之前写了篇关于Swagger2的博文 Swagger2生成在线接口文档并导出pdf文件,可以把swagger在线接口文档导出成pdf文件本地查看,当然swagger2本身是支持导出markdown格式的,所以这两种格式没啥问题了。 现在有个需求是这样的:把接口和接口描述放在表格里呈现出来,那么要么是excel的表格,要么是word的表格,两种格式都试一下

一、Excel格式

第三方的项目源码在这里:https://github.com/ghdefe/swagger-json-to-csv,下载下来导入到Idea

核心代码如下:

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 hm = new HashMap(); JsonNode node = jsonNode.findValue("paths"); Iterator stringIterator = node.fieldNames(); while (stringIterator.hasNext()) { //api JsonNode tags = node.findValue((api = stringIterator.next())); Iterator 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(); //当前查询到的一个接口数据放到hashmap里管理 Root root = new Root(name, method, api,description); if (hm.containsKey(root.getName())) { List roots = hm.get(root.getName()); roots.add(root); hm.put(root.getName(), roots); } else { ArrayList roots = new ArrayList(); roots.add(root); hm.put(root.getName(), roots); } } } /** * 获得name的顺序,并按顺序写入csv */ File file = new File("result.csv"); //excel不能读取utf-8编码的csv文件 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file), "GBK")); Iterator names = jsonNode.findValue("tags").iterator(); while (names.hasNext()) { String name = names.next().findValue("name").asText(); Iterator 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:/Export/result.csv"); System.out.println("done"); } }

此方法输入的是swagger json文件(api-docs),比如说你的swagger在线接口文档地址为 http://localhost:8080/doc.html,同时你的swagger配置文件里设置了groupName(“test”),那么你的swaager json在线地址为 http://localhost:8080/v2/api-docs?group=test,把这个链接下的内容复制下来就是该方法的输入文件,即代码中的1.txt 此方法输出的结果是一个csv文件,表格形式列出接口,如下图 在这里插入图片描述

二、Word格式

word格式就比excel要稍微复杂一点了 同样我们使用第三方的轮子,https://github.com/JMCuixy/swagger2word 改一下templates文件夹下的模板文件,来生成我需要的word格式接口文档 使用起来也很方便了,直接使用上一步生成csv的源文件1.txt,当然直接用http://localhost:8080/v2/api-docs?group=test地址也行更方便 在这里插入图片描述 点击try it out,执行接口,可能需要一点时间,执行完成后,点击下载 在这里插入图片描述 不合适的可以更改模板样式,模板文件在这里 在这里插入图片描述 打开word文件查看效果

三、参考博文

1、Swagger文档转Word 文档

2、提取swagger内容到csv表格,excel可打开



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有