import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ApiDocConverter {
public static void main(String[] args) throws Exception {
String json = "{\n" +
" \"paths\": {\n" +
" \"/dev-api/system/subjectResult/exportUserList\": {\n" +
" \"post\": {\n" +
" \"tags\": [\n" +
" \"bd-subject-result-controller\"\n" +
" ],\n" +
" \"summary\": \"导出对应成绩信息\",\n" +
" \"operationId\": \"exportUserListUsingPOST\",\n" +
" \"parameters\": [\n" +
" {\n" +
" \"name\": \"batch\",\n" +
" \"in\": \"query\",\n" +
" \"required\": false,\n" +
" \"style\": \"form\",\n" +
" \"schema\": {\n" +
" \"type\": \"string\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"name\": \"year\",\n" +
" \"in\": \"query\",\n" +
" \"required\": false,\n" +
" \"style\": \"form\",\n" +
" \"schema\": {\n" +
" \"type\": \"string\"\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" },\n" +
" \"/dev-api/system/subjectResult/test/{subjectId}\": {\n" +
" \"get\": {\n" +
" \"parameters\": [\n" +
" {\n" +
" \"name\": \"subjectId\",\n" +
" \"in\": \"path\",\n" +
" \"description\": \"subjectId\",\n" +
" \"required\": true,\n" +
" \"style\": \"form\",\n" +
" \"schema\": {\n" +
" \"type\": \"integer\",\n" +
" \"format\": \"int64\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"name\": \"number\",\n" +
" \"in\": \"query\",\n" +
" \"description\": \"number\",\n" +
" \"required\": true,\n" +
" \"style\": \"form\",\n" +
" \"schema\": {\n" +
" \"type\": \"number\",\n" +
" \"format\": \"float\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"name\": \"sort\",\n" +
" \"in\": \"query\",\n" +
" \"description\": \"sort\",\n" +
" \"required\": true,\n" +
" \"style\": \"form\",\n" +
" \"schema\": {\n" +
" \"type\": \"number\",\n" +
" \"format\": \"float\"\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(json);
StringBuilder result = new StringBuilder();
JsonNode pathsNode = rootNode.path("paths");
pathsNode.fieldNames().forEachRemaining(path -> {
JsonNode pathNode = pathsNode.path(path);
pathNode.fieldNames().forEachRemaining(method -> {
JsonNode methodNode = pathNode.path(method);
result.append("### ").append(method.toUpperCase()).append(" request with dynamic variables\n");
result.append(method.toUpperCase()).append(" http://{{host}}/{{staticurl}}").append(path.replace("{", "{{").replace("}", "}}"));
if (methodNode.has("parameters")) {
boolean firstQueryParam = true;
for (JsonNode param : methodNode.path("parameters")) {
String paramName = param.path("name").asText();
String paramIn = param.path("in").asText();
if ("query".equals(paramIn)) {
if (firstQueryParam) {
result.append("?");
firstQueryParam = false;
} else {
result.append("&");
}
result.append(paramName).append("=").append("{{").append(paramName).append("}}");
}
}
}
result.append("\nAccept: application/json\n\n");
});
});
System.out.println(result.toString());
}
}
标签:JsonNode,Http,name,idea,json,result,query,path,append
From: https://www.cnblogs.com/kakaBluce/p/18473959