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


    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提取更加简便快捷。
  • 相关阅读:
    智能交通监控
    YOLOV4知识点分析(二)
    YOLOV4知识点分析(一)
    错误日志写入到本地磁盘(lock 队列)
    $.each(data, function (index, value) { })的用法;json和list<>的互相转换
    ArraySegment 的使用 【转载】
    Ajax往后台传参数,无参数,一个参数,多个参数,一个对象等
    在gridview里查找模板里的button控件
    数据可视化之PowerQuery篇(十六)使用Power BI进行流失客户分析
    数据可视化之PowerQuery篇(十五)如何使用Power BI计算新客户数量?
  • 原文地址:https://www.cnblogs.com/xiaojiluben/p/13565690.html
Copyright © 2020-2023  润新知