spring解析swagger.json
在项⽬中需要把所有微服务的接⼝统⼀管理,swagger上虽然能看到,但是只能分服务,在⼀个项⽬中可能就需要多个服务在pom中导⼊
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
直接上源码
public class SwaggerJsonUtil {
public static String getHost(JSONObject swaggerJson) {
/
/JSONObject swaggerJson = JSON.parseObject(swaggerJsonStr,Feature.DisableCircularReferenceDetect);        String("host");
}
public static SwaggerInfoVo getInfo(JSONObject swaggerJson) {
Object("info", SwaggerInfoVo.class);
}
/**
* 解析所有的接⼝
* @param swaggerJson
* @return
*/
public static List<PathInfoVo> getPathInfo(JSONObject swaggerJson){
JSONObject paths = JSONObject("paths");
List<PathInfoVo> list = new ArrayList<>();
//所有的definitions
Map<String,JSONObject> refMap = getDefinitions(swaggerJson);
if(paths != null) {
Iterator<Entry<String, Object>> it = Set().iterator();
while(it.hasNext()) {
PathInfoVo pathInfo = new PathInfoVo();
Entry<String, Object> path = it.next();
String pathUrl = Key();
pathInfo.setPathUrl(pathUrl);
//请求⽅式
JSONObject pathJson = JSONObject(pathUrl);
Set<String> methodSet = pathJson.keySet();
//当⽅法⽀持多种请求⽅式时也只取第⼀种
if(CollectionUtils.isNotEmpty(methodSet)) {
String httpMethod = methodSet.iterator().next();
pathInfo.setHttpMethod(httpMethod);
JSONObject methodJson = JSONObject(httpMethod);
String summary = String("summary");
String operationId = String("operationId");
String description = String("description");
pathInfo.setDescription(description);
pathInfo.setOperationId(operationId);
pathInfo.setSummary(summary);
JSONArray parameters = JSONArray("parameters");
JSONObject responses = JSONObject("responses");
List<ParameterVo> reqParameters = getParameter(parameters,refMap);
pathInfo.setReqList(reqParameters);
List<ResponseVo> respList = getResponse(responses,refMap);
pathInfo.setRespList(respList);
}
list.add(pathInfo);
}
}
return list;
}
/**
* 解析响应数据
* @param responses
* @param refMap
* @return
*/
public static List<ResponseVo> getResponse(JSONObject responses,Map<String,JSONObject> refMap){        List<ResponseVo> respParameters = new ArrayList<>();
if(responses != null && ainsKey("200")) {
//只解析200的数据
JSONObject successJson = JSONObject("200");
ainsKey("schema")) {
JSONObject schema = JSONObject("schema");
String schemaType = String("type");
String ref = "";
ainsKey("$ref")) {
ref = String("$ref");
}
if("array".equalsIgnoreCase(schemaType)){
JSONObject items = JSONObject("items");
ainsKey("$ref")) {
ref = String("$ref");
}else {
ResponseVo resp = new ResponseVo();
resp.setName("");
resp.String("type"));
respParameters.add(resp);
}
}
if(StringUtils.isNotBlank(ref)) {
String def = ref.substring(14);
JSONObject defJson = (def);
//    String type = String("type");
JSONObject properties = JSONObject("properties");
Set<String> respKeys = properties.keySet();
for(String key : respKeys) {
JSONObject respMap = JSONObject(key);
ResponseVo resp = new ResponseVo();
resp.setName(key);
resp.String("format"));
resp.String("description"));
String respType = String("type");
resp.setType(StringUtils.isBlank(respType)?"object":respType);
resp.BooleanValue("required"));
ainsKey("$ref")) {
String childRef = String("$ref");
String childDef = childRef.substring(14);
JSONObject childDefJson = (childDef);
JSONObject childProperties = JSONObject("properties");
getRef(refMap,childProperties,resp,childDef,childDefJson);
}else if("array".equalsIgnoreCase(respType)) {
JSONObject items = JSONObject("items");
ainsKey("$ref")) {
String itemRef = String("$ref");
String itemDef = itemRef.substring(14);
JSONObject itemDefJson = (itemDef);
JSONObject childProperties = JSONObject("properties");
getRef(refMap,childProperties,resp,itemDef,itemDefJson);
}
}
respParameters.add(resp);
}
}
}
}
return respParameters;
}
/**
* 递归响应数据
* @param refMap  所有的definitions
* @param respMap  当前需要解析的JSON对象
* @param parentVoName  上级ref的名称,与上级相同不继续递归(树结构)
* @return
*/
public static List<ResponseVo> getRef(Map<String,JSONObject> refMap,JSONObject childProperties,ResponseVo parentResp,String parentVoName,JSONObject childJson) {
Set<String> childSet = childProperties.keySet();
List<ResponseVo> childResp = new ArrayList<>();
for(String key : childSet) {
JSONObject childMap = JSONObject(key);
ResponseVo resp = new ResponseVo();
resp.setName(key);
resp.String("format"));
resp.String("description"));
String childType = String("type");
resp.setType(StringUtils.isNotBlank(childType)?String("type"));
resp.BooleanValue("required"));
childResp.add(resp);
parentResp.setChildResp(childResp);
ainsKey("$ref")) {
String childRef = String("$ref");
String childDef = childRef.substring(14);
JSONObject childDefJson = (childDef);
JSONObject pro = JSONObject("properties");
//additionalProperties
if(pro != null && !childDef.equalsIgnoreCase(parentVoName)) {
getRef(refMap,pro,resp,childDef,childDefJson);
}
}else if("array".equalsIgnoreCase(childType)) {
JSONObject items = JSONObject("items");
ainsKey("$ref")) {
String itemRef = String("$ref");
String itemDef = itemRef.substring(14);
JSONObject itemDefJson = (itemDef);
JSONObject pro = JSONObject("properties");
if(pro != null && !itemDef.equalsIgnoreCase(parentVoName)) {
getRef(refMap,pro,resp,itemDef,itemDefJson);
}
}
}
}
return childResp;
}
/**
* 解析请求参数
* @param parameters
* @param refMap
* @return
*/
public static List<ParameterVo> getParameter(JSONArray parameters,Map<String,JSONObject> refMap){        List<ParameterVo> reqParameters = new ArrayList<>();
if(CollectionUtils.isNotEmpty(parameters)) {
for(int i = 0; i < parameters.size(); i ++) {
微服务在哪里JSONObject paramJson = JSONObject(i);
ParameterVo param = JSON.JSONString(paramJson),ParameterVo.class);
ainsKey("schema")) {
JSONObject schema = JSONObject("schema");
String schemaType = String("type");
String ref = "";
ainsKey("$ref")) {
ref = String("$ref");
}
if("array".equalsIgnoreCase(schemaType)){
JSONObject items = JSONObject("items");
ainsKey("$ref")) {
ref = String("$ref");
}else {
List<ParameterVo> childParamList = new ArrayList<>();
ParameterVo childParam = new ParameterVo();
childParam.setName("");
childParam.String("type"));
childParamList.add(childParam);
param.setChildParam(childParamList);
}
}else {
param.setType(schemaType);
}
if(StringUtils.isNotBlank(ref)) {
String def = ref.substring(14);
JSONObject defJson = (def);
if(defJson != null) {
param.String("type"));
JSONObject properties = JSONObject("properties");
Set<String> propertiesSet = properties.keySet();
List<ParameterVo> childParamList = new ArrayList<>();
for(String key : propertiesSet) {
ParameterVo childParam = new ParameterVo();
childParam.setName(key);
JSONObject proMap = JSONObject(key);
//根据type判断是否是array
String type = String("type");
childParam.setDescription(StringUtils.String("description"))?
childParam.setType(StringUtils.isBlank(type)?"object":type);
childParam.String("format"));
childParam.BooleanValue("required"));
ainsKey("$ref")) {
String childRef = String("$ref");
String childDef = childRef.substring(14);
JSONObject childDefJson = (childDef);
JSONObject childProperties = JSONObject("properties");
if(childProperties != null) {
getParamRef(refMap,childProperties,childParam,childDef,childDefJson);
}
}else if("array".equalsIgnoreCase(type)) {
JSONObject items = JSONObject("items");
ainsKey("$ref")) {
String itemRef = String("$ref");
String itemDef = itemRef.substring(14);
JSONObject itemDefJson = (itemDef);
JSONObject pro = JSONObject("properties");
if(pro != null) {
getParamRef(refMap,pro,childParam,itemDef,itemDefJson);
}
}
}
childParamList.add(childParam);
param.setChildParam(childParamList);
}
}
}
}
reqParameters.add(param);
}
}
return reqParameters;
}
public static List<ParameterVo> getParamRef(Map<String,JSONObject> refMap,JSONObject childProperties,ParameterVo parentResp,String parentVoName,JSONObject childJson) {
List<ParameterVo> paramList = new ArrayList<>();
Set<String> childSet = childProperties.keySet();
for(String key : childSet) {
JSONObject childMap = JSONObject(key);
ParameterVo resp = new ParameterVo();
resp.setName(key);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。