自学内容网 自学内容网

根据json转HttpClient脚本

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());


原文地址:https://blog.csdn.net/w12515114096/article/details/143031539

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!