POSTMAN JSON转接口文档 Collection v2.1 (recommended)
作者:互联网
本代码主要针对“GET”请求和“POST”请求中的 raw->body(json)模式,且导出版本对应Collection v2.1
所需jar包
ExportPostManApiUtil.java
package mango; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; /** * postman转接口文档html * zhouy * 2021-6-10 11:45:48 */ 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\\acer\\Desktop\\test.json"); } /** * 解析postman 导出的Json数据 * * @param path */ @SuppressWarnings({ "unchecked", "rawtypes" }) private static void readFile(String path) { String file = readToString(path); Map<String,Object> fileMap = (Map<String,Object>)JSON.parse(file); Map<String,Object> infoMap = (Map<String,Object>)fileMap.get("info"); List<Map> apiList = JSONObject.parseArray(fileMap.get("item").toString(), Map.class); writeToHtml(infoMap.get("name").toString(), apiList); } /** * 数据拼成html * * @param title * @param gameList */ @SuppressWarnings("rawtypes") private static void writeToHtml(String title, List<Map> apiList) { PrintWriter out = null; try { StringBuffer buffer = new StringBuffer(); buffer.append("<html>"); buffer.append("<style type='text/css'>td{padding: 10px;}</style>"); buffer.append("<body>"); buffer.append("<h2>" + title + "</h2>"); for (Map apiMap : apiList) { Map requestMap = (Map) apiMap.get("request"); Map urlMap = (Map) requestMap.get("url"); List<String> pathStringList = JSONObject.parseArray(urlMap.get("path").toString(), String.class); //取出方法名 String methodNameStr = pathStringList.get(pathStringList.size()-1).split("\\.")[0]; buffer.append("<br/>"); buffer.append("<h3 style='margin:1px 0 1px 0'>接口名称:" + parseGet(apiMap.get("name")) + "</h3>"); buffer.append("<br/>"); buffer.append("<h3 style='margin:1px 0 1px 0'>方法名称:" + methodNameStr + "</h3>"); buffer.append("<br/>"); buffer.append("<h3 style='margin:1px 0 1px 0'>接口地址:" + parseGet(urlMap.get("raw")) + "</h3>"); buffer.append("<br/>"); buffer.append("<h3 style='margin:1px 0 1px 0'>请求方式:" + parseGet(requestMap.get("method")) + "</h3>"); buffer.append("<br/>"); if(String.valueOf(requestMap.get("method")).toLowerCase().equals("get")) { if(urlMap.get("query") != null ) { List<Map> queryMap = JSONObject.parseArray(urlMap.get("query").toString(), Map.class); if (null != queryMap && queryMap.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 (Map data : queryMap) { buffer.append("<tr>"); buffer.append("<td>" + parseGet(data.get("key")) + "</td><td>String</td><td>是</td><td>" + parseGet(data.get("value")) + "</td><td>" + parseGet(data.get("description")) + "</td>"); buffer.append("</tr>"); } buffer.append("</table>"); } } }else { if(requestMap.get("body")!= null ) { Map bodyMap = (Map) requestMap.get("body"); buffer.append("<h3 style='margin:1px 0 1px 0'>请求参数:</h3>"); buffer.append("<br/>"); buffer.append("<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" style='table-layout: fixed;width:60%'>"); buffer.append("<tr>"); buffer.append("<td style='width:10%'>请求方式</td><td style='width:90%'>JSON格式</td>"); buffer.append("</tr>"); buffer.append("<tr>"); buffer.append("<td>" + parseGet(bodyMap.get("mode")) + "</td><td style='word-break:break-all;word-wrap:break-word;white-space:pre-wrap;'>" + parseGet(bodyMap.get("raw")) + "</td>"); buffer.append("</tr>"); buffer.append("</table>"); } } buffer.append("<br/>"); buffer.append("<br/>"); buffer.append("<br/>"); } buffer.append("</body>"); buffer.append("</html>"); //写入html文件并输出 out = new PrintWriter("C:\\Users\\acer\\Desktop\\加密卡接口测试.html"); out.write(buffer.toString()); //数据写入成功 System.out.println("----------------------->数据写入成功"); } catch (Exception e) { e.printStackTrace(); }finally { if(out!=null) { out.close(); } } } /** * 非空处理 * @param value * @return */ public static String parseGet(Object value) { if(value == null) { return "<空>"; }else { return String.valueOf(value); } } }
参考:https://www.cnblogs.com/xsnd/p/8708817.html
标签:Map,POSTMAN,get,buffer,Collection,recommended,import,append,String 来源: https://www.cnblogs.com/zhoumango/p/14870510.html