resourceList) {
+ this.resourceList = resourceList;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
}
diff --git a/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/configuration/EnableSwaggerMgUi.java b/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/configuration/EnableSwaggerMgUi.java
index 4b2563bb..21adfe14 100644
--- a/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/configuration/EnableSwaggerMgUi.java
+++ b/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/configuration/EnableSwaggerMgUi.java
@@ -1,26 +1,29 @@
-package com.zyplayer.doc.swagger.framework.configuration;
-
-import java.lang.annotation.Documented;
-import java.lang.annotation.Retention;
-import java.lang.annotation.Target;
-
-import org.springframework.context.annotation.Import;
-
-@Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
-@Target(value = { java.lang.annotation.ElementType.TYPE })
-@Documented
-@Import({ SwaggerCommonConfiguration.class, SpringContextUtil.class })
-public @interface EnableSwaggerMgUi {
-
- /**
- * 是否自动把自身的swagger-resources加进来
- * @return 配置
- */
- boolean selfDoc() default true;
-
- /**
- * 启动后第一次访问没有数据情况下需要加载进来的swagger-resources地址
- * @return swagger-resources地址
- */
- String[] defaultResources() default {};
-}
+package com.zyplayer.doc.swagger.framework.configuration;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import com.zyplayer.doc.swagger.framework.filter.ZyplayerProxyFilter;
+import org.springframework.context.annotation.Import;
+
+@Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
+@Target(value = {java.lang.annotation.ElementType.TYPE})
+@Documented
+@Import({SwaggerCommonConfiguration.class, SpringContextUtil.class, ZyplayerProxyFilter.class})
+public @interface EnableSwaggerMgUi {
+
+ /**
+ * 是否自动把自身的swagger-resources加进来
+ *
+ * @return 配置
+ */
+ boolean selfDoc() default true;
+
+ /**
+ * 启动后第一次访问没有数据情况下需要加载进来的swagger-resources地址
+ *
+ * @return swagger-resources地址
+ */
+ String[] defaultResources() default {};
+}
diff --git a/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/configuration/SwaggerCommonConfiguration.java b/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/configuration/SwaggerCommonConfiguration.java
index 0817e4ae..e68e4c58 100644
--- a/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/configuration/SwaggerCommonConfiguration.java
+++ b/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/configuration/SwaggerCommonConfiguration.java
@@ -1,25 +1,25 @@
-package com.zyplayer.doc.swagger.framework.configuration;
-
-import org.springframework.context.annotation.ComponentScan;
-
-//@EnableAutoConfiguration
-@ComponentScan(basePackages = {
- "com.zyplayer.doc.swagger.controller",
- "com.zyplayer.doc.swagger.framework.service",
-})
-public class SwaggerCommonConfiguration {
-
-// 不再默认开启拦截
-// @Autowired
-// private MgUiTestFilter mgUiTestFilter;
-//
-// @Bean
-// public FilterRegistrationBean mockTestFilter() {
-// FilterRegistrationBean registration = new FilterRegistrationBean();
-// registration.setFilter(mgUiTestFilter);
-// registration.addUrlPatterns("/*");
-// registration.setName("mgUiTestFilter");
-// registration.setOrder(2);
-// return registration;
-// }
-}
+package com.zyplayer.doc.swagger.framework.configuration;
+
+import org.springframework.context.annotation.ComponentScan;
+
+//@EnableAutoConfiguration
+@ComponentScan(basePackages = {
+ "com.zyplayer.doc.swagger.controller",
+ "com.zyplayer.doc.swagger.framework.service",
+})
+public class SwaggerCommonConfiguration {
+
+// 不再默认开启拦截
+// @Autowired
+// private ZyplayerApiTestFilter mgUiTestFilter;
+//
+// @Bean
+// public FilterRegistrationBean mockTestFilter() {
+// FilterRegistrationBean registration = new FilterRegistrationBean();
+// registration.setFilter(mgUiTestFilter);
+// registration.addUrlPatterns("/*");
+// registration.setName("mgUiTestFilter");
+// registration.setOrder(2);
+// return registration;
+// }
+}
diff --git a/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/constant/Consts.java b/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/constant/Consts.java
new file mode 100644
index 00000000..44be0bef
--- /dev/null
+++ b/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/constant/Consts.java
@@ -0,0 +1,8 @@
+package com.zyplayer.doc.swagger.framework.constant;
+
+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";
+}
diff --git a/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/filter/MgUiTestFilter.java b/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/filter/ZyplayerApiTestFilter.java
similarity index 92%
rename from zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/filter/MgUiTestFilter.java
rename to zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/filter/ZyplayerApiTestFilter.java
index 62594356..19d69f3e 100644
--- a/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/filter/MgUiTestFilter.java
+++ b/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/filter/ZyplayerApiTestFilter.java
@@ -19,8 +19,8 @@ import com.zyplayer.doc.swagger.framework.service.MgStorageService;
* 有需要此拦截器的请自行拷贝至自身项目,需要使过滤器生效,文档不再开启@Component
* 判断是否是模拟请求,功能需求:
* 很多时候后端定义好了接口,但还未实现,这时前端已经需要数据调试了,这时就需要用到这个过滤器了!
- * 在页面上先配置好模拟返回的数据,然后在url上加入参数:mgUiTestFlag=1
- * 例:http://192.168.0.249:8082/openApi/case/info?mgUiTestFlag=1
+ * 在页面上先配置好模拟返回的数据,然后在url上加入参数:zyplayerApiTest=1
+ * 例:http://192.168.0.249:8082/openApi/case/info?zyplayerApiTest=1
* 本过滤器就直接返回了之前配置的模拟数据,而不用等到后端必须把接口实现之后才能调试,或者在前端写一大段测试数据。
*
* 例:笔者的公司后端人较少,一个需求需要10个接口,需求分析完后首先就把接口、参数、返回值定义好,然后一个个的去实现。
@@ -28,7 +28,7 @@ import com.zyplayer.doc.swagger.framework.service.MgStorageService;
* 而不是一味的催后台,把各种锅丢给后端,然后玩自己的去了,浪费各环节等待时间。
*/
//@Component
-public class MgUiTestFilter implements Filter {
+public class ZyplayerApiTestFilter implements Filter {
@Autowired
private MgStorageService mgStorageService;
@@ -41,7 +41,7 @@ public class MgUiTestFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// 需要使用模拟值返回的标记
- String mockTestFlag = request.getParameter("mgUiTestFlag");
+ String mockTestFlag = request.getParameter("zyplayerApiTest");
if (!"1".equals(mockTestFlag)) {
// 未开启直接跳过
chain.doFilter(request, response);
diff --git a/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/filter/ZyplayerProxyFilter.java b/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/filter/ZyplayerProxyFilter.java
new file mode 100644
index 00000000..b2090546
--- /dev/null
+++ b/zyplayer-doc-swagger/src/main/java/com/zyplayer/doc/swagger/framework/filter/ZyplayerProxyFilter.java
@@ -0,0 +1,166 @@
+package com.zyplayer.doc.swagger.framework.filter;
+
+import cn.hutool.http.HttpRequest;
+import com.alibaba.fastjson.JSON;
+import com.zyplayer.doc.swagger.controller.param.HttpRequestParam;
+import com.zyplayer.doc.swagger.controller.vo.SwaggerResourcesInfoVo;
+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.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.servlet.*;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.net.HttpCookie;
+import java.util.*;
+
+/**
+ * 请求代理过滤器
+ * 使得所有的swagger-ui都可以查看所有的文档,目标是:文档大一统~
+ *
+ * @author 暮光:城中城
+ * @since 2019年12月16日
+ */
+@Component
+public class ZyplayerProxyFilter implements Filter {
+
+ private static final Logger logger = LoggerFactory.getLogger(ZyplayerProxyFilter.class);
+
+ @Autowired
+ private MgStorageService mgStorageService;
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+
+ }
+
+ @Override
+ public void destroy() {
+
+ }
+
+ public static void main(String[] args) {
+// String requestUrl = "http://192.168.0.249:8082/zyplayer-doc-manage/zyplayer-proxy/1/v2/api-docs";
+// int proxyIndex = requestUrl.indexOf(Consts.ZYPLAYER_PROXY);
+// if (proxyIndex < 0) {
+// return;
+// }
+// String requestUrlEnd = requestUrl.substring(proxyIndex + Consts.ZYPLAYER_PROXY.length());
+// int idIndex = requestUrlEnd.indexOf("/");
+// String idStr = requestUrlEnd.substring(0, idIndex);
+
+ String resource = "{\"swagger\":\"2.0\",\"host\":\"127.0.0.1:8999\",\"basePath\":\"/\",\"tags\":";
+ resource = resource.replaceFirst("\"basePath\":\".*?\"", "\"basePath\":\"/xxxxx\"");
+ System.out.println(resource);
+ }
+
+ @Override
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
+ HttpServletRequest httpServletRequest = (HttpServletRequest) request;
+ HttpServletResponse servletResponse = (HttpServletResponse) response;
+ String method = httpServletRequest.getMethod();
+ String requestUrl = httpServletRequest.getRequestURL().toString();
+ String bodyStr = IOUtils.toString(httpServletRequest.getInputStream(), "utf-8");
+
+ int proxyIndex = requestUrl.indexOf(Consts.ZYPLAYER_PROXY);
+ if (proxyIndex < 0) {
+ // 不是拦截接口直接跳过
+ chain.doFilter(request, response);
+ return;
+ }
+ long startTime = System.currentTimeMillis();
+ String requestUrlEnd = requestUrl.substring(proxyIndex + Consts.ZYPLAYER_PROXY.length());
+ int idIndex = requestUrlEnd.indexOf("/");
+ String proxyUrlEnd = requestUrlEnd.substring(idIndex);
+ String idStr = requestUrlEnd.substring(0, idIndex);
+ Integer docId = Integer.valueOf(idStr);
+
+ String swaggerResourcesStr = mgStorageService.get(StorageKeys.SWAGGER_RESOURCES_LIST);
+ List resourcesInfoVoList = JSON.parseArray(swaggerResourcesStr, SwaggerResourcesInfoVo.class);
+ if (resourcesInfoVoList == null || resourcesInfoVoList.isEmpty()) {
+ this.send(startTime, requestUrl, servletResponse, "文档为空,请刷新重试");
+ return;
+ }
+ // 找到真正被代理的对象
+ String proxyUrl = null;
+ for (SwaggerResourcesInfoVo swaggerResourcesInfoVo : resourcesInfoVoList) {
+ if (Objects.equals(swaggerResourcesInfoVo.getId(), docId)) {
+ proxyUrl = swaggerResourcesInfoVo.getUrl();
+ break;
+ }
+ }
+ if (StringUtils.isBlank(proxyUrl)) {
+ this.send(startTime, requestUrl, servletResponse, "未找到对应文档,请刷新重试");
+ return;
+ }
+ // 组装URL
+ proxyUrl = proxyUrl.replace(Consts.SWAGGER_RESOURCES, proxyUrlEnd);
+ HttpRequestParam param = new HttpRequestParam();
+ param.setUrl(proxyUrl);
+ param.setMethod(method);
+ HttpRequest httpRequest = param.createRequest();
+ // 组装参数
+ Map parameterMap = httpServletRequest.getParameterMap();
+ if (parameterMap != null && parameterMap.size() > 0) {
+ for (Map.Entry entry : parameterMap.entrySet()) {
+ httpRequest.form(entry.getKey(), entry.getValue());
+ }
+ }
+ // 请求体
+ if (StringUtils.isNotBlank(bodyStr)) {
+ httpRequest.body(bodyStr);
+ }
+ // header
+ Enumeration headerNames = httpServletRequest.getHeaderNames();
+ if (headerNames != null) {
+ while (headerNames.hasMoreElements()) {
+ String element = headerNames.nextElement();
+ httpRequest.header(element, httpServletRequest.getHeader(element));
+ }
+ }
+ // cookies
+ Cookie[] cookies = httpServletRequest.getCookies();
+ if (cookies != null && cookies.length > 0) {
+ List httpCookieList = new LinkedList<>();
+ for (int i = 0; i < cookies.length; i++) {
+ HttpCookie httpCookie = new HttpCookie(cookies[i].getName(), cookies[i].getValue());
+ httpCookieList.add(httpCookie);
+ }
+ httpRequest.cookie(httpCookieList.toArray(new HttpCookie[]{}));
+ }
+ String resultStr = httpRequest.execute().body();
+ if (proxyUrl.indexOf(Consts.V2_API_DOCS) >= 0) {
+ // "basePath":"/" 替换成 "basePath":"/zyplayer-doc-manage/zyplayer-proxy/2/",使其走代理接口
+ ServletContext servletContext = httpServletRequest.getServletContext();
+ Object ctx = servletContext.getAttribute("ctx");
+ ctx = (ctx + Consts.ZYPLAYER_PROXY + idStr).replaceAll("//", "/");
+ resultStr = resultStr.replaceFirst("\"basePath\":\".*?\"", "\"basePath\":\"" + ctx + "\"");
+ // 替换host为当前项目的
+ String requestUrlHost = requestUrl.substring(0, proxyIndex + Consts.ZYPLAYER_PROXY.length());
+ requestUrlHost = requestUrlHost.replace("http://", "");
+ requestUrlHost = requestUrlHost.substring(0, requestUrlHost.indexOf("/"));
+ resultStr = resultStr.replaceFirst("\"host\":\".*?\"", "\"host\":\"" + requestUrlHost + "\"");
+ }
+ this.send(startTime, proxyUrl, servletResponse, resultStr);
+ }
+
+ public void send(Long startTime, String requestUrl, HttpServletResponse response, String result) throws IOException {
+ response.setStatus(200);
+ // response.setContentType("application/json");
+ response.addHeader("Access-Control-Allow-Origin", "*");
+ response.setContentType("text/html");
+ response.setCharacterEncoding("UTF-8");
+ response.setHeader("Cache-Control", "no-cache, must-revalidate");
+ response.getWriter().write(result);
+ Long totalTime = System.currentTimeMillis() - startTime;
+ logger.error("代理请求结束,总耗时:{}ms,URI:{}", totalTime, requestUrl);
+ }
+}
diff --git a/zyplayer-doc-swagger/src/main/resources/document.html b/zyplayer-doc-swagger/src/main/resources/document.html
index 20925aea..09d8a365 100644
--- a/zyplayer-doc-swagger/src/main/resources/document.html
+++ b/zyplayer-doc-swagger/src/main/resources/document.html
@@ -300,8 +300,8 @@
| 功能说明 |
- 访问接口时增加参数:mgUiTestFlag=1,即可返回下面提交的模拟返回值
- 需要被访问项目增加 MgUiTestFilter.java
+ 访问接口时增加参数:zyplayerApiTest=1,即可返回下面提交的模拟返回值
+ 需要被访问项目增加 ZyplayerApiTestFilter.java
过滤器才能使用,里面逻辑很简单,一看就懂~
|
diff --git a/zyplayer-doc-swagger/src/main/resources/webjars/mg-ui/js/mg-ui.js b/zyplayer-doc-swagger/src/main/resources/webjars/mg-ui/js/mg-ui.js
index a663915c..0a8798a5 100644
--- a/zyplayer-doc-swagger/src/main/resources/webjars/mg-ui/js/mg-ui.js
+++ b/zyplayer-doc-swagger/src/main/resources/webjars/mg-ui/js/mg-ui.js
@@ -334,8 +334,8 @@ $("#apiPathTree").on("click", ".show-doc", function(){
$("#postUrlInput").val(data.domain + docUrl);
// 处理模拟返回
$("#simulationResultUrl").text(docUrl);
- $("#simulationResultUrlTest").text(data.domain + docUrl + "?mgUiTestFlag=1");
- $("#simulationResultUrlTest").attr("href", data.domain + docUrl + "?mgUiTestFlag=1");
+ $("#simulationResultUrlTest").text(data.domain + docUrl + "?zyplayerApiTest=1");
+ $("#simulationResultUrlTest").attr("href", data.domain + docUrl + "?zyplayerApiTest=1");
$("#simulationResultText").val("");
getStorage('p-simulation-response-' + docUrl, function(data){
var resultText = getNotEmptyStr(data);