swagger支持json文档录入

This commit is contained in:
暮光:城中城
2019-08-12 20:44:17 +08:00
parent f9173925ab
commit 26b700fde3
10 changed files with 134 additions and 15 deletions

View File

@@ -11,15 +11,13 @@ import com.zyplayer.doc.swagger.controller.vo.SwaggerLocationVo;
import com.zyplayer.doc.swagger.controller.vo.SwaggerResourcesInfoVo;
import com.zyplayer.doc.swagger.framework.configuration.EnableDocSwagger;
import com.zyplayer.doc.swagger.framework.configuration.SpringContextUtil;
import com.zyplayer.doc.swagger.framework.constant.Consts;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger.web.SwaggerResource;
import javax.annotation.Resource;
@@ -300,7 +298,12 @@ public class MgDocumentController {
* @return 添加结果
*/
@PostMapping(value = "/addSwaggerResources")
public ResponseJson<Object> addSwaggerResources(String resourcesUrl, String rewriteDomainUrl, String oldUrl, Integer openVisit) {
public ResponseJson<Object> addSwaggerResources(HttpServletRequest request, String swaggerJson, String resourcesUrl, String rewriteDomainUrl, String oldUrl, Integer openVisit) {
// 通过json文档内容来添加
ResponseJson<Object> responseJson = this.addSwaggerJsonApiDocs(request, swaggerJson, resourcesUrl);
if (responseJson != null) {
return responseJson;
}
// 总有些人喜欢填这个地址,那就替换来支持下吧
int htmlIndex = resourcesUrl.indexOf("/swagger-ui.html");
if (htmlIndex > 0) {
@@ -352,6 +355,38 @@ public class MgDocumentController {
return DocResponseJson.ok();
}
/**
* 增加json格式文档
*
* @author 暮光:城中城
* @since 2019年8月11日
* @param swaggerJson swagger的文档内容
* @return 添加结果
*/
public ResponseJson<Object> addSwaggerJsonApiDocs(HttpServletRequest request, String swaggerJson, String resourcesUrl) {
if (StringUtils.isNotBlank(swaggerJson)) {
Integer nextId = 0;
String customUrl = resourcesUrl;
if (resourcesUrl.contains(Consts.PROXY_API_DOCS)) {
nextId = Integer.valueOf(resourcesUrl.substring(resourcesUrl.indexOf("?id=") + 4));
} else {
nextId = storageService.getNextId();
customUrl = request.getRequestURL().toString();
customUrl = customUrl.substring(0, customUrl.lastIndexOf("/"));
customUrl = customUrl + Consts.PROXY_API_DOCS + "?id=" + nextId;
}
boolean addResult = this.addSwaggerLocationList(swaggerJson, null, customUrl, customUrl, 0);
if (addResult) {
storageService.put(StorageKeys.PROXY_API_DOCS + nextId, swaggerJson);
return DocResponseJson.ok();
}
return DocResponseJson.warn("添加失败");
} else if (resourcesUrl.contains(Consts.PROXY_API_DOCS)) {
return DocResponseJson.warn("添加失败,该地址是代理地址,必须填写文档内容");
}
return null;
}
/**
* 增加/swagger-resources地址
*
@@ -403,6 +438,11 @@ public class MgDocumentController {
String locationDel = this.encodeUrlParam(location);
locationList = locationList.stream().filter(val -> !Objects.equals(val.getLocation(), locationDel)).collect(Collectors.toList());
this.storageSwaggerLocationList(locationList);
// 代理地址判断
if (location.contains(Consts.PROXY_API_DOCS)) {
Integer nextId = Integer.valueOf(location.substring(location.indexOf("?id=") + 4));
storageService.remove(StorageKeys.PROXY_API_DOCS + nextId);
}
return DocResponseJson.ok();
}

View File

@@ -10,10 +10,7 @@ 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.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@@ -100,6 +97,23 @@ public class MgOpenDocController {
DocResponseJson.ok(swaggerResourceStrList).send(response);
}
/**
* 获取增加的json格式文档
*
* @author 暮光:城中城
* @since 2019年8月11日
* @param id swagger的文档内容的id
* @return 添加结果
*/
@GetMapping(value = "/proxyApiDocs")
public String getApiDocs(Long id) {
String result = storageService.get(StorageKeys.PROXY_API_DOCS + id);
if (StringUtils.isBlank(result)) {
return DocResponseJson.warn("未找到该文档").toJson();
}
return result;
}
/**
* 全局参数
*/

View File

@@ -0,0 +1,46 @@
package com.zyplayer.doc.swagger.controller;
import com.zyplayer.doc.core.json.DocResponseJson;
import com.zyplayer.doc.swagger.framework.constant.Consts;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* 接口文档代理返回控制器
*
* @author 暮光:城中城
* @since 2019年8月11日
*/
@RestController
@RequestMapping("/swagger-mg-ui/document")
public class MgProxyApiDocController {
private static Logger logger = LoggerFactory.getLogger(MgProxyApiDocController.class);
@Resource
private MgStorageService storageService;
/**
* 获取增加的json格式文档
*
* @param id swagger的文档内容的id
* @return 结果
* @author 暮光:城中城
* @since 2019年8月11日
*/
@GetMapping(value = Consts.PROXY_API_DOCS)
public String getApiDocs(Long id) {
String result = storageService.get(StorageKeys.PROXY_API_DOCS + id);
if (StringUtils.isBlank(result)) {
return DocResponseJson.warn("未找到该文档").toJson();
}
return result;
}
}

View File

@@ -5,4 +5,5 @@ public class Consts {
public static final String ZYPLAYER_PROXY = "/zyplayer-proxy/";
public static final String V2_API_DOCS = "/v2/api-docs";
public static final String SWAGGER_RESOURCES = "/swagger-resources";
public static final String PROXY_API_DOCS = "/proxy-api-docs";
}

View File

@@ -19,4 +19,6 @@ public class StorageKeys {
public static final String SWAGGER_ID_WORKER = "swagger-id-worker";
// 全局参数
public static final String GLOBAL_PARAM_LIST = "zyplayer-doc-global-param-list";
// 代理的文档内容
public static final String PROXY_API_DOCS = "proxy-api-docs-";
}