swagger文档树展示开发

This commit is contained in:
暮光:城中城
2021-10-26 22:32:42 +08:00
parent 1d999710bb
commit 395090e958
18 changed files with 424 additions and 71 deletions

View File

@@ -80,15 +80,20 @@ public class SwaggerDocumentController {
if (resourceList == null || resourceList.isEmpty()) {
return DocResponseJson.warn("该地址未找到文档");
}
// 删除原有文档
if (swaggerDoc.getId() != null) {
swaggerDocService.removeById(swaggerDoc.getId());
}
// 存明细地址
String swaggerDomain = SwaggerDocUtil.getSwaggerResourceDomain(docUrl);
for (SwaggerResource resource : resourceList) {
swaggerDoc.setId(null);
swaggerDoc.setDocUrl(resource.getUrl());
swaggerDoc.setDocUrl(swaggerDomain + resource.getUrl());
swaggerDoc.setName(resource.getName());
swaggerDocService.save(swaggerDoc);
}
} else if (SwaggerDocUtil.isSwaggerLocation(docUrl)) {
swaggerDocService.save(swaggerDoc);
swaggerDocService.saveOrUpdate(swaggerDoc);
} else {
return DocResponseJson.warn("不支持的地址:" + docUrl);
}

View File

@@ -1,23 +1,33 @@
package com.zyplayer.doc.swaggerplus.controller;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.zyplayer.doc.core.annotation.AuthMan;
import com.zyplayer.doc.core.exception.ConfirmException;
import com.zyplayer.doc.core.json.DocResponseJson;
import com.zyplayer.doc.core.json.ResponseJson;
import com.zyplayer.doc.data.repository.manage.entity.SwaggerDoc;
import com.zyplayer.doc.data.service.manage.SwaggerDocService;
import com.zyplayer.doc.swaggerplus.service.SwaggerHttpRequestService;
import io.swagger.models.Swagger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;
import springfox.documentation.swagger.web.ApiKeyVehicle;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.UiConfiguration;
import javax.annotation.Resource;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;
/**
* 承接了所有的ApiResourceController的接口
@@ -29,21 +39,39 @@ import java.util.Set;
@RestController
public class SwaggerProxyController {
private static final String HAL_MEDIA_TYPE = "application/hal+json";
@Resource
private SwaggerDocService swaggerDocService;
@Resource
private SwaggerHttpRequestService swaggerHttpRequestService;
@RequestMapping("/swagger-resources")
public List<SwaggerResource> swaggerResources() {
Set<SwaggerResource> resourceList = new HashSet<>();
List<SwaggerResource> resourceList = new LinkedList<>();
List<SwaggerDoc> docList = swaggerDocService.getSwaggerDocList(new SwaggerDoc());
for (SwaggerDoc swaggerDoc : docList) {
SwaggerResource resource = new SwaggerResource();
resource.setUrl("/doc-swagger/doc/content?id=" + swaggerDoc.getId());
resource.setUrl("/v2/api-docs?id=" + swaggerDoc.getId());
resource.setName(swaggerDoc.getName());
resource.setSwaggerVersion("2.0");
resourceList.add(resource);
}
return new LinkedList<>(resourceList);
return resourceList;
}
@ResponseBody
@RequestMapping(value = "/v2/api-docs", produces = {MimeTypeUtils.APPLICATION_JSON_VALUE, HAL_MEDIA_TYPE})
public ResponseEntity<Json> content(HttpServletRequest request, Long id) {
SwaggerDoc swaggerDoc = swaggerDocService.getById(id);
if (swaggerDoc == null) {
throw new ConfirmException("文档不存在");
}
if (Objects.equals(swaggerDoc.getDocType(), 1)) {
String contentStr = swaggerHttpRequestService.requestUrl(request, swaggerDoc.getDocUrl());
return new ResponseEntity<>(new Json(contentStr), HttpStatus.OK);
}
return new ResponseEntity<>(new Json(swaggerDoc.getJsonContent()), HttpStatus.OK);
}
@ResponseBody

View File

@@ -14,6 +14,14 @@ public class SwaggerDocUtil {
return docUrl.contains("/swagger-resources");
}
public static String getSwaggerResourceDomain(String docUrl) {
int index = docUrl.indexOf("/swagger-resources");
if (index >= 0) {
return docUrl.substring(0, index);
}
return "";
}
public static boolean isSwaggerLocation(String docUrl) {
return docUrl.contains("/v2/api-docs");
}