This commit is contained in:
songhaozhi 2021-06-26 15:45:13 +08:00
commit 3ba32a65f2
7 changed files with 43 additions and 20 deletions

View File

@ -202,7 +202,7 @@ smart-doc官方目前已经开发完成[Maven插件](https://gitee.com/smart-doc
"apiConstants": [{//从1.8.9开始配置自己的常量类smart-doc在解析到常量时自动替换为具体的值非必须
"constantsClassName": "com.power.doc.constants.RequestParamConstant"
}],
"responseBodyAdvice":{ //自smart-doc 1.9.8起ResponseBodyAdvice统一返回设置可用ignoreResponseBodyAdvice tag来忽略
"responseBodyAdvice":{ //自smart-doc 1.9.8起,非必须项,ResponseBodyAdvice统一返回设置(不要随便配置根据项目的技术来配置)可用ignoreResponseBodyAdvice tag来忽略
"className":"com.power.common.model.CommonResult" //通用响应体
},
"requestBodyAdvice":{ ////自smart-doc 2.1.4 起支持设置RequestBodyAdvice统一请求包装类非必须
@ -211,8 +211,8 @@ smart-doc官方目前已经开发完成[Maven插件](https://gitee.com/smart-doc
}
```
>上面的JSON配置实例中只有"outPath"是必填项。其他额外配置项请根据实际情况来配置,胡乱复制上面的全配置到自己的项目中
可能引起使用错误
>上面的JSON配置实例中只有"outPath"是必填项。其他额外配置项请根据实际情况来配置,请不要胡乱复制上面的全配置到自己的项目中
可能引起使用错误
**注意:** 对于老用户完全可以通过`Fastjson`或者是`Gson`库将`ApiConfig`转化成JSON配置。

View File

@ -22,8 +22,6 @@
*/
package com.power.doc.builder;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.power.common.util.CollectionUtil;
import com.power.common.util.FileUtil;
import com.power.common.util.StringUtil;
@ -32,6 +30,7 @@ import com.power.doc.constants.Methods;
import com.power.doc.model.*;
import com.power.doc.template.SpringBootDocBuildTemplate;
import com.power.doc.utils.DocUtil;
import com.power.doc.utils.JsonUtil;
import com.thoughtworks.qdox.JavaProjectBuilder;
import java.util.*;
@ -90,8 +89,7 @@ public class OpenApiBuilder {
String filePath = config.getOutPath();
filePath = filePath + DocGlobalConstants.OPEN_API_JSON;
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String data = gson.toJson(json);
String data = JsonUtil.toPrettyJson(json);
FileUtil.nioWriteFile(data, filePath);
}

View File

@ -22,8 +22,6 @@
*/
package com.power.doc.builder;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.power.common.util.CollectionUtil;
import com.power.common.util.FileUtil;
import com.power.common.util.StringUtil;
@ -39,6 +37,7 @@ import com.power.doc.model.postman.request.body.BodyBean;
import com.power.doc.model.postman.request.header.HeaderBean;
import com.power.doc.template.IDocBuildTemplate;
import com.power.doc.template.SpringBootDocBuildTemplate;
import com.power.doc.utils.JsonUtil;
import com.power.doc.utils.PathUtil;
import com.thoughtworks.qdox.JavaProjectBuilder;
@ -255,8 +254,7 @@ public class PostmanJsonBuilder {
requestItem.setItem(itemBeans);
String filePath = config.getOutPath();
filePath = filePath + DocGlobalConstants.POSTMAN_JSON;
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String data = gson.toJson(requestItem);
String data = JsonUtil.toPrettyJson(requestItem);
FileUtil.nioWriteFile(data, filePath);
}

View File

@ -91,7 +91,7 @@ public class JsonBuildHelper {
}
return StringUtil.removeQuotes(DocUtil.jsonValueByType(typeName));
}
return JsonFormatUtil.formatJson(buildJson(typeName, returnType, Boolean.TRUE, 0, new HashMap<>(), builder));
return JsonUtil.toPrettyFormat(buildJson(typeName, returnType, Boolean.TRUE, 0, new HashMap<>(), builder));
}
/**

View File

@ -188,7 +188,7 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
Map<String, String> authorMap = DocUtil.getParamsComments(method, DocTags.AUTHOR, cls.getName());
String authorValue = String.join(", ", new ArrayList<>(authorMap.keySet()));
if (apiConfig.isShowAuthor() && StringUtil.isNotEmpty(authorValue)) {
apiMethodDoc.setAuthor(JsonFormatUtil.formatJson(authorValue));
apiMethodDoc.setAuthor(JsonUtil.toPrettyFormat(authorValue));
}
if (apiConfig.isShowAuthor() && StringUtil.isEmpty(authorValue)) {
apiMethodDoc.setAuthor(classAuthor);
@ -395,10 +395,10 @@ public class SpringBootDocBuildTemplate implements IDocBuildTemplate<ApiDoc> {
.append("\":")
.append(DocUtil.handleJsonStr(mockValue))
.append("}");
requestExample.setJsonBody(JsonFormatUtil.formatJson(builder.toString())).setJson(true);
requestExample.setJsonBody(JsonUtil.toPrettyFormat(builder.toString())).setJson(true);
} else {
String json = JsonBuildHelper.buildJson(typeName, gicTypeName, Boolean.FALSE, 0, new HashMap<>(), configBuilder);
requestExample.setJsonBody(JsonFormatUtil.formatJson(json)).setJson(true);
requestExample.setJsonBody(JsonUtil.toPrettyFormat(json)).setJson(true);
}
paramAdded = true;
} else if (SpringMvcAnnotations.PATH_VARIABLE.contains(annotationName)) {

View File

@ -0,0 +1,32 @@
package com.power.doc.utils;
import com.google.gson.*;
/**
* @author yu 2021/6/26.
*/
public class JsonUtil {
/**
* Convert a JSON string to pretty print
* @param jsonString json string
* @return Format json string
*/
public static String toPrettyFormat(String jsonString) {
JsonObject json = JsonParser.parseString(jsonString).getAsJsonObject();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String prettyJson = gson.toJson(json);
return prettyJson;
}
/**
* Convert a JSON to String and pretty print
* @param src Json
* @return Format json string
*/
public static String toPrettyJson(Object src) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String prettyJson = gson.toJson(src);
return prettyJson;
}
}

View File

@ -2,24 +2,19 @@ package com.power.doc.utils;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.internal.$Gson$Types;
import com.power.common.model.EnumDictionary;
import com.power.common.util.CollectionUtil;
import com.power.common.util.StringUtil;
import com.power.doc.builder.BaseDocBuilderTemplate;
import com.power.doc.constants.TornaConstants;
import com.power.doc.model.*;
import com.power.doc.model.rpc.RpcApiDependency;
import com.power.doc.model.torna.*;
import okhttp3.internal.http2.ErrorCode;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static com.power.doc.constants.TornaConstants.PUSH;
/**
* @author xingzi 2021/4/28 16:15
**/