增加开放文档逻辑,优化文档展示
This commit is contained in:
@@ -32,7 +32,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 文档控制器
|
||||
*
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
*/
|
||||
@@ -46,7 +46,7 @@ public class MgDocumentController {
|
||||
|
||||
/**
|
||||
* 获取所有的文档地址
|
||||
*
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
* @return 文档内容
|
||||
@@ -301,7 +301,7 @@ public class MgDocumentController {
|
||||
|
||||
/**
|
||||
* 增加/swagger-resources地址
|
||||
*
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
* @param resourcesUrl swagger-resources地址
|
||||
@@ -341,6 +341,8 @@ public class MgDocumentController {
|
||||
logger.error("获取文档失败:{},{}", resourcesUrl, e.getMessage());
|
||||
return DocResponseJson.warn("该地址查找文档失败");
|
||||
}
|
||||
// 去重
|
||||
resourcesList = resourcesList.stream().distinct().collect(Collectors.toList());
|
||||
storageService.put(StorageKeys.SWAGGER_RESOURCES_LIST, JSON.toJSONString(resourcesList));
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
@@ -379,7 +381,7 @@ public class MgDocumentController {
|
||||
|
||||
/**
|
||||
* 删除/v2/api-docs
|
||||
*
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
* @param docUrl 文档地址
|
||||
@@ -489,6 +491,10 @@ public class MgDocumentController {
|
||||
* url编码参数
|
||||
*/
|
||||
private String encodeUrlParam(String resourcesUrl) {
|
||||
if (StringUtils.isBlank(resourcesUrl)) {
|
||||
return resourcesUrl;
|
||||
}
|
||||
resourcesUrl = resourcesUrl.trim();
|
||||
int indexOf = resourcesUrl.indexOf("?");
|
||||
if (indexOf < 0) {
|
||||
return resourcesUrl;
|
||||
|
||||
@@ -1,15 +1,24 @@
|
||||
package com.zyplayer.doc.swagger.controller;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.zyplayer.doc.core.json.DocResponseJson;
|
||||
import com.zyplayer.doc.core.json.ResponseJson;
|
||||
import com.zyplayer.doc.swagger.controller.vo.LocationListVo;
|
||||
import com.zyplayer.doc.swagger.controller.vo.SwaggerResourcesInfoVo;
|
||||
import com.zyplayer.doc.swagger.framework.constant.StorageKeys;
|
||||
import com.zyplayer.doc.swagger.framework.service.MgStorageService;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 开放接口文档控制器
|
||||
@@ -31,8 +40,64 @@ public class MgOpenDocController {
|
||||
*/
|
||||
@ResponseBody
|
||||
@PostMapping(value = "/{source}")
|
||||
public ResponseJson<List<SwaggerResourcesInfoVo>> resourcesList(@PathVariable String source) {
|
||||
public ResponseJson<List<SwaggerResourcesInfoVo>> resourcesList(@PathVariable("source") String source) {
|
||||
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取所有的文档
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
* @param request request
|
||||
* @param response response
|
||||
*/
|
||||
@ResponseBody
|
||||
@PostMapping(value = "/docs")
|
||||
public void docs(HttpServletRequest request, HttpServletResponse response) {
|
||||
String choiceLocationList = request.getParameter("choiceLocationList");
|
||||
List<LocationListVo> locationList = new LinkedList<>();
|
||||
String swaggerLocationListStr = storageService.get(StorageKeys.SWAGGER_LOCATION_LIST);
|
||||
if (StringUtils.isNotBlank(swaggerLocationListStr)) {
|
||||
locationList = JSON.parseArray(swaggerLocationListStr, LocationListVo.class);
|
||||
}
|
||||
if (StringUtils.isNotBlank(choiceLocationList)) {
|
||||
locationList = locationList.stream().filter(val -> Objects.equals(val.getUuid(), choiceLocationList)).collect(Collectors.toList());
|
||||
} else {
|
||||
DocResponseJson.ok().send(response);
|
||||
}
|
||||
if (locationList.size() <= 0) {
|
||||
DocResponseJson.ok().send(response);
|
||||
}
|
||||
List<String> swaggerResourceStrList = new LinkedList<>();
|
||||
for (LocationListVo location : locationList) {
|
||||
try {
|
||||
String resourceStr = HttpRequest.get(location.getLocation()).timeout(3000).execute().body();
|
||||
Map<String, Object> jsonObject = JSON.parseObject(resourceStr, new TypeReference<HashMap<String, Object>>(){});
|
||||
if (jsonObject == null || jsonObject.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
jsonObject.put("fullUrl", location);
|
||||
String resourcesUrl = location.getLocation();
|
||||
int indexV2 = location.getLocation().indexOf("/v2");
|
||||
if (indexV2 >= 0) {
|
||||
resourcesUrl = location.getLocation().substring(0, indexV2);
|
||||
}
|
||||
// 本来想转对象之后赋值,但是在此转成JSON字符串之后格式就不是之前的了,所有不能转。。。
|
||||
// 直接字符串拼接,坑真多~
|
||||
String rewriteDomainUrl = Optional.ofNullable(location.getRewriteDomainUrl()).orElse("");
|
||||
resourceStr = resourceStr.substring(1);
|
||||
resourceStr = "{\"fullUrl\":\"" + location.getLocation() + "\","
|
||||
+ "\"domainUrl\":\"" + resourcesUrl + "\","
|
||||
+ "\"rewriteDomainUrl\":\"" + rewriteDomainUrl + "\","
|
||||
+ resourceStr;
|
||||
swaggerResourceStrList.add(resourceStr);
|
||||
} catch (Exception e) {
|
||||
logger.error("获取文档失败:{},{}", location, e.getMessage());
|
||||
}
|
||||
}
|
||||
// 用默认的json解析要内存溢出,解析不了JSONObject、、就只有这样写了~
|
||||
DocResponseJson.ok(swaggerResourceStrList).send(response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package com.zyplayer.doc.swagger.controller.vo;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
|
||||
import java.net.URLDecoder;
|
||||
import java.util.Objects;
|
||||
|
||||
public class LocationListVo {
|
||||
private Integer id;
|
||||
private String name;
|
||||
private String uuid;
|
||||
private String location;
|
||||
private String resources;
|
||||
private String rewriteDomainUrl;
|
||||
@@ -18,6 +21,7 @@ public class LocationListVo {
|
||||
this.location = location;
|
||||
this.resources = resources;
|
||||
this.name = location;
|
||||
this.uuid = RandomUtil.simpleUUID();
|
||||
// 修正名字
|
||||
int indexGroup = location.indexOf("group=");
|
||||
int indexV2 = location.indexOf("/v2");
|
||||
@@ -84,4 +88,12 @@ public class LocationListVo {
|
||||
public void setRewriteDomainUrl(String rewriteDomainUrl) {
|
||||
this.rewriteDomainUrl = rewriteDomainUrl;
|
||||
}
|
||||
|
||||
public String getUuid() {
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void setUuid(String uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import springfox.documentation.swagger.web.SwaggerResource;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class SwaggerResourcesInfoVo implements Serializable {
|
||||
private Integer id;
|
||||
@@ -89,4 +90,17 @@ public class SwaggerResourcesInfoVo implements Serializable {
|
||||
public void setRewriteDomainUrl(String rewriteDomainUrl) {
|
||||
this.rewriteDomainUrl = rewriteDomainUrl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
return Objects.equals(this.url, ((SwaggerResourcesInfoVo) obj).getUrl());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return url.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user