Postman導出Api文檔

1、最近離職要把作搞過的接口整理成文檔,查了查postman好像不支持導出文檔,因而寫了個工具類,供你們參考!html

  前提你要先把postman裏的接口導出來java

  如圖:json

  

 

 

2、所用到的包(主要Json相關的包)和項目結構api

  commons-beanutils-1.9.2.jar,commons-collections-3.2.1.jar,commons-httpclient-3.1-sources.jar,app

  commons-lang-2.6.jar,commons-logging-1.2.jar,ezmorph-1.0.6.jar,json-lib-2.4.jar,morph-1.1.1.jar工具

  

  

 

3、代碼post

  

package zf;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Yan on 2018/4/3.
 */
public class ExportPostManApiUtil {

    public static String readToString(String fileName) {
        String encoding = "UTF-8";
        File file = new File(fileName);
        Long fileLength = file.length();
        byte[] fileContent = new byte[fileLength.intValue()];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(fileContent);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            return new String(fileContent, encoding);
        } catch (UnsupportedEncodingException e) {
            System.err.println("The OS does not support " + encoding);
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
//        readFile("C:\\Users\\Yan\\Desktop\\api\\b.json");
//        readFile("C:\\Users\\Yan\\Desktop\\api\\c.json");
//        readFile("C:\\Users\\Yan\\Desktop\\api\\d.json");
//        readFile("C:\\Users\\Yan\\Desktop\\api\\e.json");
//        readFile("C:\\Users\\Yan\\Desktop\\api\\f.json");
        readFile("C:\\Users\\Yan\\Desktop\\api\\h.json");
    }

    /**
     * 解析postman 導出的Json數據
     *
     * @param path
     */
    private static void readFile(String path) {
        String file = readToString(path);
        JSONObject allData = JSONObject.fromObject(file);
        String title = allData.getString("name");
        String requests = allData.getString("requests");

        JSONArray reqList = JSONArray.fromObject(requests);

        List<PostMan> postManList = new ArrayList<>();

        for (Object o : reqList) {
            JSONObject jo = (JSONObject) o;
            PostMan postMan = new PostMan();
            postMan.setName(jo.getString("name"));
            postMan.setUrl(jo.getString("url"));
            postMan.setMethod(jo.getString("method"));

            if (!jo.getString("data").equals("null")) {
                JSONArray dataArr = JSONArray.fromObject(jo.getString("data"));
                List<Data> dataList = new ArrayList<>();
                if (dataArr.size() > 0) {
                    for (Object d : dataArr) {
                        JSONObject jsonData = (JSONObject) d;
                        Data data = new Data();
                        data.setKey(jsonData.getString("key"));
                        data.setType(jsonData.getString("type"));
                        data.setEnabled(jsonData.getBoolean("enabled"));
                        if (jsonData.containsKey("description")) {
                            data.setDescription(jsonData.getString("description"));
                        }
                        data.setValue(jsonData.getString("value"));
                        dataList.add(data);
                    }
                }
                postMan.setData(dataList);
            }
            postManList.add(postMan);
        }
        writeToHtml(title, postManList);
    }

    /**
     * 數據拼成html
     *
     * @param title
     * @param postManList
     */
    private static void writeToHtml(String title, List<PostMan> postManList) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("<h2>" + title + "</h2>");
        buffer.append("<br/>");
        for (PostMan pm : postManList) {
            buffer.append("<h3 style='margin:1px 0 1px 0'>接口名稱:" + pm.getName() + "</h3>");
            buffer.append("<br/>");
            buffer.append("<h3 style='margin:1px 0 1px 0'>接口地址:" + pm.getUrl() + "</h3>");
            buffer.append("<br/>");
            buffer.append("<h3 style='margin:1px 0 1px 0'>請求方式:" + pm.getMethod() + "</h3>");
            buffer.append("<br/>");
            List<Data> dataList = pm.getData();
            if (null != dataList && dataList.size() > 0) {
                buffer.append("<h3 style='margin:1px 0 1px 0'>請求參數:</h3>");
                buffer.append("<br/>");

                buffer.append("<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">");
                buffer.append("<tr>");
                buffer.append("<td>參數名</td><td>參數類型</td><td>是否必填</td><td>示例值</td><td>描述</td>");
                buffer.append("</tr>");
                for (Data data : dataList) {
                    buffer.append("<tr>");
                    buffer.append("<td>" + data.getKey() + "</td><td>" + data.getType() + "</td><td>" + data.getEnabled() + "</td><td>" + data.getValue() + "</td><td>" + data.getDescription() + "</td>");
                    buffer.append("</tr>");
                }
                buffer.append("</table>");
                buffer.append("<br/>");
            }
        }
        System.out.println(buffer.toString());
    }


}
package zf;

/**
 * Created by Yan on 2018/4/3.
 */
public class Data {

    private String key;
    private String value;
    private String type;
    private Boolean enabled;
    private String description;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

 

 

package zf;

import java.util.List;

/**
 * Created by Yan on 2018/4/3.
 */
public class PostMan {

    private String name;
    private String url;
    private String method;
    private List<Data> data;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public List<Data> getData() {
        return data;
    }

    public void setData(List<Data> data) {
        this.data = data;
    }
}

 4、運行效果(須要把控制檯打印出來字符串放到html中)this

  

5、源代碼url

   https://files.cnblogs.com/files/xsnd/demo.zipspa

相關文章
相關標籤/搜索