提取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 += "\n" + a[i].getElementsByTagName("span")[0].innerText}

//請求方法
let b = document.getElementsByClassName("opblock-summary-method");
var temp = "";
for(var i=0;i<b.length;i++){
temp += "\n" + b[i].innerText}

//描述
let c = document.getElementsByClassName("opblock-summary-description");
var temp = "";
for(var i=0;i<c.length;i++){
temp += "\n" + 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提取更加簡便快捷。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM