代码整理
This commit is contained in:
@@ -0,0 +1,286 @@
|
||||
package com.zyplayer.doc.swagger.controller;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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 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.framework.configuration.EnableSwaggerMgUi;
|
||||
import com.zyplayer.doc.swagger.framework.configuration.SpringContextUtil;
|
||||
import com.zyplayer.doc.swagger.framework.constant.StorageKeys;
|
||||
import com.zyplayer.doc.swagger.framework.service.MgStorageService;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import springfox.documentation.swagger.web.SwaggerResource;
|
||||
|
||||
/**
|
||||
* 文档控制器
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/swagger-mg-ui/document")
|
||||
public class MgDocumentController {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(MgDocumentController.class);
|
||||
|
||||
@Autowired
|
||||
private MgStorageService storageService;
|
||||
|
||||
/**
|
||||
* 获取所有的文档地址
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
* @return 文档内容
|
||||
*/
|
||||
@ResponseBody
|
||||
@PostMapping(value = "/resourcesList")
|
||||
public ResponseJson<Set<String>> resourcesList() {
|
||||
String swaggerResourcesStr = storageService.get(StorageKeys.SWAGGER_RESOURCES_LIST);
|
||||
Set<String> resourcesSet = new HashSet<>();
|
||||
if (StringUtils.isNotBlank(swaggerResourcesStr)) {
|
||||
List<String> resourcesList = JSON.parseArray(swaggerResourcesStr, String.class);
|
||||
resourcesSet.addAll(resourcesList);
|
||||
}
|
||||
return DocResponseJson.ok(resourcesSet);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有的文档
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
* @param request request
|
||||
* @param response response
|
||||
*/
|
||||
@ResponseBody
|
||||
@PostMapping(value = "/docs")
|
||||
public void docs(HttpServletRequest request, HttpServletResponse response) {
|
||||
boolean needRestorage = true;
|
||||
String choiseDocList = request.getParameter("choiseDocList");
|
||||
// 转成set,防止重复
|
||||
Set<String> resourcesSet = new HashSet<>();
|
||||
Set<String> swaggerDocsDeleteSet = new HashSet<>();
|
||||
if (StringUtils.isNotBlank(choiseDocList)) {
|
||||
needRestorage = false;// 选择的则不再存入
|
||||
resourcesSet.addAll(Arrays.asList(choiseDocList.split(",")));
|
||||
} else {
|
||||
String swaggerResourcesStr = storageService.get(StorageKeys.SWAGGER_RESOURCES_LIST);
|
||||
String swaggerDocsDeleteStr = storageService.get(StorageKeys.SWAGGER_DOCS_DELETE_LIST);
|
||||
if (StringUtils.isNotBlank(swaggerResourcesStr)) {
|
||||
List<String> resourcesList = JSON.parseArray(swaggerResourcesStr, String.class);
|
||||
resourcesSet.addAll(resourcesList);
|
||||
} else {
|
||||
// 默认加上自身的文档
|
||||
String serverPath = "http://" + request.getServerName() // 服务器地址
|
||||
+ ":" + request.getServerPort() // 端口号
|
||||
+ request.getContextPath();
|
||||
// 是否加入自身的文档
|
||||
Object object = SpringContextUtil.getBeanWithAnnotation(EnableSwaggerMgUi.class);
|
||||
EnableSwaggerMgUi swaggerMgUi = object.getClass().getAnnotation(EnableSwaggerMgUi.class);
|
||||
if (swaggerMgUi == null) {
|
||||
// 直接通过superclass去找
|
||||
Class<?> superclass = object.getClass().getSuperclass();
|
||||
if (superclass != null) {
|
||||
swaggerMgUi = superclass.getAnnotation(EnableSwaggerMgUi.class);
|
||||
}
|
||||
}
|
||||
if (swaggerMgUi == null) {
|
||||
// 再通过AopUtils去找
|
||||
Class<?> targetClass = AopUtils.getTargetClass(object);
|
||||
if (targetClass != null) {
|
||||
swaggerMgUi = targetClass.getAnnotation(EnableSwaggerMgUi.class);
|
||||
}
|
||||
}
|
||||
if (swaggerMgUi == null) {
|
||||
resourcesSet.add(serverPath + "/swagger-resources");
|
||||
} else {
|
||||
if (swaggerMgUi.selfDoc()) {
|
||||
resourcesSet.add(serverPath + "/swagger-resources");
|
||||
}
|
||||
// 启动后第一次访问没有数据情况下需要加载进来的swagger-resources地址
|
||||
String[] defaultResources = swaggerMgUi.defaultResources();
|
||||
if (defaultResources != null && defaultResources.length > 0) {
|
||||
resourcesSet.addAll(Arrays.asList(defaultResources));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (StringUtils.isNotBlank(swaggerDocsDeleteStr)) {
|
||||
List<String> swaggerDocsDeleteList = JSON.parseArray(swaggerDocsDeleteStr, String.class);
|
||||
swaggerDocsDeleteSet.addAll(swaggerDocsDeleteList);
|
||||
}
|
||||
}
|
||||
List<Map<String, Object>> swaggerResourceList = new LinkedList<>();
|
||||
List<String> swaggerResourceStrList = new LinkedList<>();
|
||||
for (String resourcesUrl : resourcesSet) {
|
||||
List<SwaggerResource> resourceList = null;
|
||||
try {
|
||||
String resourcesStr = HttpRequest.get(resourcesUrl).timeout(3000).execute().body();
|
||||
resourceList = JSON.parseArray(resourcesStr, SwaggerResource.class);
|
||||
} catch (Exception e) {
|
||||
logger.error("获取文档失败:{},{}", resourcesUrl, e.getMessage());
|
||||
}
|
||||
if (resourceList == null || resourceList.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
resourcesUrl = resourcesUrl.substring(0, resourcesUrl.lastIndexOf("/") + 1);
|
||||
for (SwaggerResource resource : resourceList) {
|
||||
String location = resource.getLocation();
|
||||
// 最后一个斜杠在resourcesUrl中已经加上,替换掉后面的防止两根斜杠
|
||||
location = location.startsWith("/") ? location.replaceFirst("/", "") : location;
|
||||
if (location.indexOf("?") >= 0) {
|
||||
try {
|
||||
String encode = URLEncoder.encode(resource.getName(), "utf-8");
|
||||
location = location.substring(0, location.lastIndexOf("?")) + "?group=" + encode;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
location = resourcesUrl + location;
|
||||
// 已删除的则不处理
|
||||
if (swaggerDocsDeleteSet.contains(location)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
String resourceStr = HttpRequest.get(location).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);
|
||||
swaggerResourceList.add(jsonObject);
|
||||
// 本来想转对象之后赋值,但是在此转成JSON字符串之后格式就不是之前的了,所有不能转。。。
|
||||
// 直接字符串拼接,坑真多~
|
||||
resourceStr = resourceStr.substring(1);
|
||||
resourceStr = "{\"fullUrl\":\"" + location + "\","
|
||||
+ "\"domainUrl\":\"" + resourcesUrl + "\","
|
||||
+ resourceStr;
|
||||
swaggerResourceStrList.add(resourceStr);
|
||||
} catch (Exception e) {
|
||||
logger.error("获取文档失败:{},{}", location, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (needRestorage) {
|
||||
storageService.put(StorageKeys.SWAGGER_RESOURCES_LIST, JSON.toJSONString(resourcesSet));
|
||||
}
|
||||
// 用默认的json解析要内存溢出,解析不了JSONObject、、就只有这样写了~
|
||||
DocResponseJson.ok(swaggerResourceStrList).send(response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加/swagger-resources地址
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
* @param resourcesUrl swagger-resources地址
|
||||
* @return 添加结果
|
||||
*/
|
||||
@PostMapping(value = "/addSwaggerResources")
|
||||
public ResponseJson<Object> addSwaggerResources(String resourcesUrl) {
|
||||
String swaggerResourcesStr = storageService.get(StorageKeys.SWAGGER_RESOURCES_LIST);
|
||||
String swaggerDocsDeleteStr = storageService.get(StorageKeys.SWAGGER_DOCS_DELETE_LIST);
|
||||
Set<String> swaggerDocsDeleteSet = new HashSet<>();
|
||||
if (StringUtils.isNotBlank(swaggerDocsDeleteStr)) {
|
||||
List<String> swaggerDocsDeleteList = JSON.parseArray(swaggerDocsDeleteStr, String.class);
|
||||
swaggerDocsDeleteSet.addAll(swaggerDocsDeleteList);
|
||||
}
|
||||
// 转成set,防止重复
|
||||
Set<String> resourcesSet = new HashSet<>();
|
||||
if (StringUtils.isNotBlank(swaggerResourcesStr)) {
|
||||
List<String> resourcesList = JSON.parseArray(swaggerResourcesStr, String.class);
|
||||
resourcesSet.addAll(resourcesList);
|
||||
}
|
||||
String resourcesStr = null;
|
||||
try {
|
||||
resourcesStr = HttpRequest.get(resourcesUrl).timeout(3000).execute().body();
|
||||
List<SwaggerResource> resourceList = JSON.parseArray(resourcesStr, SwaggerResource.class);
|
||||
if (resourceList == null || resourceList.isEmpty()) {
|
||||
return DocResponseJson.warn("该地址未找到文档");
|
||||
}
|
||||
// 重新加入的时候把之前的已删除的回恢复
|
||||
String resourcesDomain = resourcesUrl.substring(0, resourcesUrl.lastIndexOf("/") + 1);
|
||||
for (SwaggerResource swaggerResource : resourceList) {
|
||||
String location = swaggerResource.getLocation();
|
||||
// 最后一个斜杠在resourcesUrl中已经加上,替换掉后面的防止两根斜杠
|
||||
location = location.startsWith("/") ? location.replaceFirst("/", "") : location;
|
||||
if (location.indexOf("?") >= 0) {
|
||||
try {
|
||||
String encode = URLEncoder.encode(swaggerResource.getName(), "utf-8");
|
||||
location = location.substring(0, location.lastIndexOf("?")) + "?group=" + encode;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
location = resourcesDomain + location;
|
||||
swaggerDocsDeleteSet.remove(location);
|
||||
}
|
||||
resourcesSet.add(resourcesUrl);
|
||||
} catch (Exception e) {
|
||||
// 暂不想支持直接添加地址
|
||||
// try {
|
||||
// SwaggerLocationVo swaggerLocationVo = JSON.parseObject(resourcesStr, SwaggerLocationVo.class);
|
||||
// if (StringUtils.isNotBlank(swaggerLocationVo.getSwagger())) {
|
||||
// Set<String> locationSet = new HashSet<>();
|
||||
// if (StringUtils.isNotBlank(swaggerLocationListStr)) {
|
||||
// String swaggerLocationListStr = storageService.get(StorageKeys.SWAGGER_LOCATION_LIST);
|
||||
// List<String> locationList = JSON.parseArray(swaggerLocationListStr, String.class);
|
||||
// locationSet.addAll(locationList);
|
||||
// storageService.put(StorageKeys.SWAGGER_LOCATION_LIST, JSON.toJSONString(locationSet));
|
||||
// }
|
||||
// } else {
|
||||
// return DocResponseJson.warn("该地址查找文档失败");
|
||||
// }
|
||||
// } catch (Exception e2) {
|
||||
logger.error("获取文档失败:{},{}", resourcesUrl, e.getMessage());
|
||||
return DocResponseJson.warn("该地址查找文档失败");
|
||||
// }
|
||||
}
|
||||
storageService.put(StorageKeys.SWAGGER_RESOURCES_LIST, JSON.toJSONString(resourcesSet));
|
||||
storageService.put(StorageKeys.SWAGGER_DOCS_DELETE_LIST, JSON.toJSONString(swaggerDocsDeleteSet));
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除/v2/api-docs
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
* @param docUrl 文档地址
|
||||
* @return 删除结果
|
||||
*/
|
||||
@PostMapping(value = "/deleteSwaggerDoc")
|
||||
public ResponseJson<Object> deleteSwaggerDoc(String docUrl) {
|
||||
String swaggerDocsDeleteStr = storageService.get(StorageKeys.SWAGGER_DOCS_DELETE_LIST);
|
||||
Set<String> swaggerDocsDeleteSet = new HashSet<>();
|
||||
if (StringUtils.isNotBlank(swaggerDocsDeleteStr)) {
|
||||
List<String> swaggerDocsDeleteList = JSON.parseArray(swaggerDocsDeleteStr, String.class);
|
||||
swaggerDocsDeleteSet.addAll(swaggerDocsDeleteList);
|
||||
}
|
||||
swaggerDocsDeleteSet.add(docUrl);
|
||||
storageService.put(StorageKeys.SWAGGER_DOCS_DELETE_LIST, JSON.toJSONString(swaggerDocsDeleteSet));
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.zyplayer.doc.swagger.controller;
|
||||
|
||||
import java.net.HttpCookie;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.zyplayer.doc.core.json.DocResponseJson;
|
||||
import com.zyplayer.doc.swagger.controller.param.HttpRequestParam;
|
||||
import com.zyplayer.doc.swagger.controller.vo.HttpCookieVo;
|
||||
import com.zyplayer.doc.swagger.controller.vo.HttpHeaderVo;
|
||||
import com.zyplayer.doc.swagger.controller.vo.HttpRequestVo;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
|
||||
/**
|
||||
* 后台代理网络请求的控制器
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/swagger-mg-ui/http")
|
||||
public class MgHttpRequestController {
|
||||
|
||||
@PostMapping(value = "/request")
|
||||
public DocResponseJson<HttpRequestVo> post(HttpRequestParam param) {
|
||||
HttpRequest request = param.createRequest();
|
||||
HttpResponse response = request.execute();
|
||||
HttpRequestVo httpRequestVo = new HttpRequestVo();
|
||||
httpRequestVo.setData(response.body());
|
||||
httpRequestVo.setStatus(response.getStatus());
|
||||
List<HttpCookie> cookies = response.getCookies();
|
||||
if (cookies != null && cookies.size() > 0) {
|
||||
List<HttpCookieVo> cookie = cookies.stream().map(val -> {
|
||||
return new HttpCookieVo(val.getName(), val.getValue());
|
||||
}).collect(Collectors.toList());
|
||||
httpRequestVo.setCookie(cookie);
|
||||
}
|
||||
Map<String, List<String>> headers = response.headers();
|
||||
if (headers != null && headers.size() > 0) {
|
||||
List<HttpHeaderVo> header = new ArrayList<>(headers.size());
|
||||
for (Entry<String, List<String>> httpHeader : headers.entrySet()) {
|
||||
HttpHeaderVo vo = new HttpHeaderVo();
|
||||
vo.setName(httpHeader.getKey());
|
||||
vo.setValue(String.join(";", httpHeader.getValue()));
|
||||
header.add(vo);
|
||||
}
|
||||
httpRequestVo.setHeader(header);
|
||||
}
|
||||
return DocResponseJson.ok(httpRequestVo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.zyplayer.doc.swagger.controller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.zyplayer.doc.core.json.DocResponseJson;
|
||||
import com.zyplayer.doc.swagger.framework.service.MgStorage;
|
||||
import com.zyplayer.doc.swagger.framework.service.MgStorageService;
|
||||
|
||||
/**
|
||||
* 后台存储服务控制器
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/swagger-mg-ui/storage")
|
||||
public class MgStorageController {
|
||||
|
||||
@Autowired
|
||||
private MgStorageService storageService;
|
||||
|
||||
@PostMapping(value = "/checkConfig")
|
||||
public DocResponseJson<Object> checkConfig() {
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
|
||||
@PostMapping(value = "/data")
|
||||
public DocResponseJson<Object> setData(String key, String value) {
|
||||
if (key == null || value == null) {
|
||||
return DocResponseJson.warn("参数名或值不能为空");
|
||||
}
|
||||
storageService.put(key, value);
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
|
||||
@GetMapping(value = "/data")
|
||||
public DocResponseJson<String> getData(String key) {
|
||||
if (key == null) {
|
||||
return DocResponseJson.warn("参数名不能为空");
|
||||
}
|
||||
String value = storageService.get(key);
|
||||
value = (value == null) ? "" : value;
|
||||
return DocResponseJson.ok(value);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/delete")
|
||||
public DocResponseJson<Object> delete(String key) {
|
||||
if (key == null) {
|
||||
return DocResponseJson.warn("参数名不能为空");
|
||||
}
|
||||
storageService.remove(key);
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
|
||||
@PostMapping(value = "/like")
|
||||
public DocResponseJson<Object> like(String key, String value) {
|
||||
List<MgStorage> likeList = storageService.like(key, value);
|
||||
return DocResponseJson.ok(likeList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
package com.zyplayer.doc.swagger.controller.param;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.Method;
|
||||
|
||||
/**
|
||||
* 请求参数对象
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
*/
|
||||
public class HttpRequestParam {
|
||||
private String url;
|
||||
private String method;
|
||||
private String header;
|
||||
private String form;
|
||||
private String body;
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
public void setHeader(String header) {
|
||||
this.header = header;
|
||||
}
|
||||
|
||||
public String getForm() {
|
||||
return form;
|
||||
}
|
||||
|
||||
public void setForm(String form) {
|
||||
this.form = form;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public void setBody(String body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public Map<String, String> getHeaderMap() {
|
||||
if (StringUtils.isBlank(header)) {
|
||||
return null;
|
||||
}
|
||||
Map<String, String> headerMap = JSON.parseObject(header, new TypeReference<HashMap<String, String>>() {
|
||||
});
|
||||
return headerMap;
|
||||
}
|
||||
|
||||
public Map<String, Object> getFormMap() {
|
||||
if (StringUtils.isBlank(form)) {
|
||||
return null;
|
||||
}
|
||||
Map<String, Object> formMap = JSON.parseObject(form, new TypeReference<HashMap<String, Object>>() {});
|
||||
return formMap;
|
||||
}
|
||||
|
||||
public void createHttpRequest(HttpRequest request) {
|
||||
Map<String, String> headerMap = this.getHeaderMap();
|
||||
if (headerMap != null) {
|
||||
request.addHeaders(headerMap);
|
||||
if (headerMap.containsKey("Content-Type")) {
|
||||
request.contentType(headerMap.get("Content-Type"));
|
||||
}
|
||||
}
|
||||
Map<String, Object> formMap = this.getFormMap();
|
||||
if (formMap != null) {
|
||||
request.form(formMap);
|
||||
}
|
||||
if (StringUtils.isNotBlank(body) && request.getMethod() != Method.GET) {
|
||||
request.body(body);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 组装请求
|
||||
* @return HttpRequest
|
||||
*/
|
||||
public HttpRequest createRequest() {
|
||||
if("get".equalsIgnoreCase(this.method)) return get();
|
||||
if("post".equalsIgnoreCase(this.method)) return post();
|
||||
if("head".equalsIgnoreCase(this.method)) return head();
|
||||
if("options".equalsIgnoreCase(this.method)) return options();
|
||||
if("put".equalsIgnoreCase(this.method)) return put();
|
||||
if("patch".equalsIgnoreCase(this.method)) return patch();
|
||||
if("delete".equalsIgnoreCase(this.method)) return delete();
|
||||
if("trace".equalsIgnoreCase(this.method)) return trace();
|
||||
return get();
|
||||
}
|
||||
|
||||
/**
|
||||
* POST请求
|
||||
* @return HttpRequest
|
||||
*/
|
||||
public HttpRequest post() {
|
||||
HttpRequest request = HttpRequest.post(this.getUrl());
|
||||
this.createHttpRequest(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
/**
|
||||
* GET请求
|
||||
* @return HttpRequest
|
||||
*/
|
||||
public HttpRequest get() {
|
||||
HttpRequest request = HttpRequest.get(this.getUrl());
|
||||
this.createHttpRequest(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
/**
|
||||
* HEAD请求
|
||||
* @return HttpRequest
|
||||
*/
|
||||
public HttpRequest head() {
|
||||
HttpRequest request = HttpRequest.head(this.getUrl());
|
||||
this.createHttpRequest(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
/**
|
||||
* OPTIONS请求
|
||||
* @return HttpRequest
|
||||
*/
|
||||
public HttpRequest options() {
|
||||
HttpRequest request = HttpRequest.options(this.getUrl());
|
||||
this.createHttpRequest(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT请求
|
||||
* @return HttpRequest
|
||||
*/
|
||||
public HttpRequest put() {
|
||||
HttpRequest request = HttpRequest.put(this.getUrl());
|
||||
this.createHttpRequest(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH请求
|
||||
* @return HttpRequest
|
||||
*/
|
||||
public HttpRequest patch() {
|
||||
HttpRequest request = HttpRequest.patch(this.getUrl());
|
||||
this.createHttpRequest(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE请求
|
||||
* @return HttpRequest
|
||||
*/
|
||||
public HttpRequest delete() {
|
||||
HttpRequest request = HttpRequest.delete(this.getUrl());
|
||||
this.createHttpRequest(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
/**
|
||||
* TRACE请求
|
||||
* @return HttpRequest
|
||||
*/
|
||||
public HttpRequest trace() {
|
||||
HttpRequest request = HttpRequest.trace(this.getUrl());
|
||||
this.createHttpRequest(request);
|
||||
return request;
|
||||
}
|
||||
|
||||
public String getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
public void setMethod(String method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.zyplayer.doc.swagger.controller.vo;
|
||||
|
||||
/**
|
||||
* cookie返回值对象
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
*/
|
||||
public class HttpCookieVo {
|
||||
private String name;
|
||||
private String value;
|
||||
|
||||
public HttpCookieVo() {
|
||||
|
||||
}
|
||||
|
||||
public HttpCookieVo(String name, String value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.zyplayer.doc.swagger.controller.vo;
|
||||
|
||||
/**
|
||||
* header返回值对象
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
*/
|
||||
public class HttpHeaderVo {
|
||||
private String name;
|
||||
private String value;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.zyplayer.doc.swagger.controller.vo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
/**
|
||||
* request返回值对象
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
*/
|
||||
public class HttpRequestVo {
|
||||
@ApiModelProperty(value = "代理请求返回的cookie")
|
||||
private List<HttpCookieVo> cookie;
|
||||
@ApiModelProperty(value = "代理请求返回的header")
|
||||
private List<HttpHeaderVo> header;
|
||||
@ApiModelProperty(value = "代理请求返回的status")
|
||||
private Integer status;
|
||||
@ApiModelProperty(value = "返回数据")
|
||||
private Object data;
|
||||
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(Object data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public List<HttpCookieVo> getCookie() {
|
||||
return cookie;
|
||||
}
|
||||
|
||||
public void setCookie(List<HttpCookieVo> cookie) {
|
||||
this.cookie = cookie;
|
||||
}
|
||||
|
||||
public List<HttpHeaderVo> getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
public void setHeader(List<HttpHeaderVo> header) {
|
||||
this.header = header;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Integer status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.zyplayer.doc.swagger.controller.vo;
|
||||
|
||||
public class SwaggerLocationVo {
|
||||
private String swagger;
|
||||
private String info;
|
||||
private String host;
|
||||
private String paths;
|
||||
private String definitions;
|
||||
|
||||
private String tags;
|
||||
private String basePath;
|
||||
|
||||
public String getSwagger() {
|
||||
return swagger;
|
||||
}
|
||||
|
||||
public void setSwagger(String swagger) {
|
||||
this.swagger = swagger;
|
||||
}
|
||||
|
||||
public String getInfo() {
|
||||
return info;
|
||||
}
|
||||
|
||||
public void setInfo(String info) {
|
||||
this.info = info;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public String getPaths() {
|
||||
return paths;
|
||||
}
|
||||
|
||||
public void setPaths(String paths) {
|
||||
this.paths = paths;
|
||||
}
|
||||
|
||||
public String getDefinitions() {
|
||||
return definitions;
|
||||
}
|
||||
|
||||
public void setDefinitions(String definitions) {
|
||||
this.definitions = definitions;
|
||||
}
|
||||
|
||||
public String getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void setTags(String tags) {
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
public String getBasePath() {
|
||||
return basePath;
|
||||
}
|
||||
|
||||
public void setBasePath(String basePath) {
|
||||
this.basePath = basePath;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
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 {};
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.zyplayer.doc.swagger.framework.configuration;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* context工具类
|
||||
*/
|
||||
@Component
|
||||
public class SpringContextUtil implements ApplicationContextAware {
|
||||
|
||||
public static ApplicationContext context;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
context = applicationContext;
|
||||
}
|
||||
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return context;
|
||||
}
|
||||
|
||||
public static <T> T getBean(Class<T> clz) {
|
||||
return context.getBean(clz);
|
||||
}
|
||||
|
||||
public static Object getBean(String string) {
|
||||
return getApplicationContext().getBean(string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类
|
||||
* @param annotationType annotation
|
||||
* @return 类对象
|
||||
*/
|
||||
public static Object getBeanWithAnnotation(Class<? extends Annotation> annotationType) {
|
||||
if (context == null) {
|
||||
return null;
|
||||
}
|
||||
Map<String, Object> beansWithAnnotation = context.getBeansWithAnnotation(annotationType);
|
||||
if(beansWithAnnotation != null && beansWithAnnotation.size() > 0) {
|
||||
for (Object element : beansWithAnnotation.values()) {
|
||||
return element;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +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;
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.zyplayer.doc.swagger.framework.constant;
|
||||
|
||||
/**
|
||||
* 存储数据的KEY常量类
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
*/
|
||||
public class StorageKeys {
|
||||
// 所有文档地址
|
||||
public static final String SWAGGER_RESOURCES_LIST = "swagger-resources-list";
|
||||
// 已删除的文档
|
||||
public static final String SWAGGER_DOCS_DELETE_LIST = "swagger-docs-delete-list";
|
||||
// 所有详细文档地址
|
||||
public static final String SWAGGER_LOCATION_LIST = "swagger-location-list";
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.zyplayer.doc.swagger.framework.constant;
|
||||
|
||||
/**
|
||||
* 提示语常量类
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
*/
|
||||
public class Toast {
|
||||
public static final String AUTOWIRED_ERROR = "暂未配置MgStorageService的实现类";
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.zyplayer.doc.swagger.framework.filter;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.zyplayer.doc.swagger.framework.service.MgStorageService;
|
||||
|
||||
/**
|
||||
* 有需要此拦截器的请自行拷贝至自身项目,不再开启@Component<br>
|
||||
* 判断是否是模拟请求,功能需求:<br>
|
||||
* 很多时候后端定义好了接口,但还未实现,这时前端已经需要数据调试了,这时就需要用到这个过滤器了!<br>
|
||||
* 在页面上先配置好模拟返回的数据,然后在url上加入参数:mgUiTestFlag=1<br>
|
||||
* 例:http://192.168.0.249:8082/openApi/case/info?mgUiTestFlag=1<br>
|
||||
* 本过滤器就直接返回了之前配置的模拟数据,而不用等到后端必须把接口实现之后才能调试,或者在前端写一大段测试数据。<br>
|
||||
*
|
||||
* 例:笔者的公司后端人较少,一个需求需要10个接口,需求分析完后首先就把接口、参数、返回值定义好,然后一个个的去实现。
|
||||
* 也许需要10天才能写完,但前端两天就写好了,急需数据看效果,这时就让他们自己去设置模拟值,加上参数自己测试好。
|
||||
* 而不是一味的催后台,把各种锅丢给后端,然后玩自己的去了,浪费各环节等待时间。
|
||||
*/
|
||||
//@Component
|
||||
public class MgUiTestFilter implements Filter {
|
||||
|
||||
@Autowired(required = false)
|
||||
private MgStorageService mgStorageService;
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||
throws IOException, ServletException {
|
||||
String mockTestFlag = request.getParameter("mgUiTestFlag");
|
||||
if (!"1".equals(mockTestFlag)) {
|
||||
chain.doFilter(request, response);
|
||||
return;
|
||||
}
|
||||
// 如果是模拟请求则直接返回模拟值
|
||||
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
|
||||
String requestUrl = httpServletRequest.getRequestURI();
|
||||
String cacheResult = mgStorageService.get("p-simulation-response-" + requestUrl);
|
||||
if (cacheResult != null) {
|
||||
responseWrite(cacheResult, (HttpServletResponse) response);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
|
||||
private static void responseWrite(String params, HttpServletResponse response) 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(params);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.zyplayer.doc.swagger.framework.service;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 数据类型
|
||||
* @author 暮光:城中城
|
||||
* @since 2018-11-27
|
||||
*/
|
||||
public class MgStorage implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String key;
|
||||
|
||||
private String value;
|
||||
|
||||
public MgStorage() {
|
||||
|
||||
}
|
||||
|
||||
public MgStorage(String key, String value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static long getSerialversionuid() {
|
||||
return serialVersionUID;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.zyplayer.doc.swagger.framework.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 实现此类才能使用服务器端的存贮功能
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月19日
|
||||
*/
|
||||
public interface MgStorageService {
|
||||
|
||||
/**
|
||||
* 获取存储的值
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月19日
|
||||
* @param key 参数
|
||||
* @return 值
|
||||
*/
|
||||
String get(String key);
|
||||
|
||||
/**
|
||||
* 模糊获取存储的值
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月19日
|
||||
* @param key 参数
|
||||
* @param value 值
|
||||
* @return 值
|
||||
*/
|
||||
List<MgStorage> like(String key, String value);
|
||||
|
||||
/**
|
||||
* 存储数据
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月19日
|
||||
* @param key 参数
|
||||
* @param value 值
|
||||
*/
|
||||
void put(String key, String value);
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月19日
|
||||
* @param key 参数
|
||||
*/
|
||||
void remove(String key);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user