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

@@ -41,6 +41,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>

View File

@@ -24,7 +24,6 @@ import java.util.Optional;
* @author 暮光:城中城
* @since 2019年7月27日
*/
@AuthMan("ES_DATASOURCE_MANAGE")
@RestController
@RequestMapping("/zyplayer-doc-es/datasource")
public class EsDatasourceController {
@@ -39,7 +38,8 @@ public class EsDatasourceController {
List<EsDatasource> datasourceList = esDatasourceService.list(wrapper);
return DocResponseJson.ok(datasourceList);
}
@AuthMan("ES_DATASOURCE_MANAGE")
@PostMapping(value = "/update")
public ResponseJson update(EsDatasource esDatasource) {
if (StringUtils.isBlank(esDatasource.getName())) {

View File

@@ -68,7 +68,7 @@ public class MgStorageServiceImpl implements MgStorageService {
ZyplayerStorage entity = new ZyplayerStorage();
entity.setDocValue(value);
UpdateWrapper<ZyplayerStorage> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq(true, "doc_key", key);
updateWrapper.eq("doc_key", key);
boolean update = zyplayerStorageService.update(entity, updateWrapper);
if (!update) {
entity = new ZyplayerStorage();

View File

@@ -45,6 +45,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>

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-";
}

View File

@@ -58,8 +58,12 @@
</div>
<div class="modal-body">
<div class="input-line">
地址:
<input v-model="addNewDocumentInput" type="text" class="form-control" placeholder="例http://127.0.0.1/swagger-resources 或 http://127.0.0.1/v2/api-docs">
文档地址:
<input v-model="addNewDocumentInput" type="text" class="form-control" :readonly="addNewDocumentInput.indexOf('proxy-api-docs') > 0" placeholder="例http://127.0.0.1/swagger-resources 或 http://127.0.0.1/v2/api-docs">
</div>
<div class="input-line">
swagger文档内容
<textarea v-model="addNewSwaggerJson" type="text" class="form-control" placeholder="swagger的json文档内容选填如果填了则不再请求上面的地址获取内容而是直接返回这里输入的"></textarea>
</div>
<div class="input-line">
重写域名地址:
@@ -94,6 +98,7 @@
data: {
swaggerLocationList: [],
addNewDocumentInput: '',
addNewSwaggerJson: '',
rewriteDomainUrl: '',
openVisit: '',
oldLocation: ''
@@ -131,12 +136,13 @@
},
addNewDocumentBtn: function(){
var addNewDocumentInput = app.addNewDocumentInput;
if(isEmpty(addNewDocumentInput)) {
if(isEmpty(addNewDocumentInput) && isEmpty(app.addNewSwaggerJson)) {
Toast.error("地址不可以为空");return;
}
var param = {
openVisit: app.openVisit ? 1 : 0,
resourcesUrl: addNewDocumentInput,
swaggerJson: app.addNewSwaggerJson,
rewriteDomainUrl: app.rewriteDomainUrl,
oldUrl: app.oldLocation
};