easyApi 导出POSTMAN文件错误修复工具
问题
导出接口配置
PostMan导入文件报错
问题是导出的文件中response这个key对应的value格式postman识别错误
解决
需要导入hutools的依赖包
package com.example.springmvc_2022_5.util;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.gson.JsonArray;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Objects;
/**
* easyApi 导出POSTMAN文件错误修复工具
* 支持jsonObject的元素移除操作
*/
public class EasyApiPostmanJsonDeal {
private static String POSTMAN_ERROR_FILE_PATH = "/Users/didi/Downloads/postman.json";
private static String[] CONTINUE_SEARCH_ELE_ARRAY = {"item"};
private String[] REMVOVE_ARRAY = {"response"};
public static void main(String[] args) throws IOException {
JSONObject jsonObject = JSONUtil
.readJSONObject(new File(POSTMAN_ERROR_FILE_PATH), Charset.defaultCharset());
EasyApiPostmanJsonDeal easyApiPostmanJsonDeal = new EasyApiPostmanJsonDeal();
for (String element : CONTINUE_SEARCH_ELE_ARRAY) {
easyApiPostmanJsonDeal.removeItems(JSONUtil.parseArray(jsonObject.get(element)));
}
System.out.println(JSONUtil.toJsonPrettyStr(jsonObject));
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(POSTMAN_ERROR_FILE_PATH);
fileWriter.write(JSONUtil.toJsonPrettyStr(jsonObject));
} catch (IOException e) {
e.printStackTrace();
} finally {
fileWriter.close();
}
}
public void removeItems(JSONArray array) {
if (Objects.isNull(array)) {
return;
}
for (Object o : array) {
if (o instanceof JsonArray) {
JSONArray jsonArray = (JSONArray) o;
removeItems(jsonArray);
} else {
JSONObject jsonObject = (JSONObject) o;
for (String element : REMVOVE_ARRAY) {
jsonObject.remove(element);
}
for (String element : CONTINUE_SEARCH_ELE_ARRAY) {
removeItems((JSONArray) jsonObject.getOrDefault(element, null));
}
}
}
}
}
最后将修复后的文件重新导入postman即可