diff --git a/README.md b/README.md index 26aa1dcc..59db2261 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,10 @@ http://127.0.0.1:8082/zyplayer-doc-manage/static/manage/home.html 4. 支持文档查找、文档编辑、在线调试接口 +5. 自动获取参数列表需要指定的类存在,所以请在 pom.xml -> dependencies 最后加上服务所在的包,后端才能通过Class.forName("xx");来找到参数列表,减少录入成本 + +6. 数组或List参数,调试参数录入格式例:[1,2] ,后端通过JSON工具转成指定类型,有不支持的类型时欢迎反馈 + ##### 四、zyplayer-doc-manage 1. 整合了上面两个功能到此项目,较少熟悉成本,`git clone`下来即可运行 diff --git a/zyplayer-doc-db/src/main/resources/doc-db.html b/zyplayer-doc-db/src/main/resources/doc-db.html index c2137a14..4d946895 100644 --- a/zyplayer-doc-db/src/main/resources/doc-db.html +++ b/zyplayer-doc-db/src/main/resources/doc-db.html @@ -3,6 +3,8 @@ + + 数据库文档 diff --git a/zyplayer-doc-db/src/main/resources/webjars/doc-db/js/doc-db.js b/zyplayer-doc-db/src/main/resources/webjars/doc-db/js/doc-db.js index db42554a..2b77f508 100644 --- a/zyplayer-doc-db/src/main/resources/webjars/doc-db/js/doc-db.js +++ b/zyplayer-doc-db/src/main/resources/webjars/doc-db/js/doc-db.js @@ -8,6 +8,8 @@ var pageConfig = { var tableRelationCharts; $(document).ready(function(){ + // 无论发布在哪、如何修改源码,请勿删除本行原作者信息,感谢 + console.log("%c项目信息:\n开发者列表:暮光:城中城\n项目地址:https://gitee.com/zyplayer/zyplayer-doc","color:red"); $('select.chosen-select').chosen({ no_results_text: '没有找到', // 当检索时没有找到匹配项时显示的提示文本 disable_search_threshold: 0, // 10 个以下的选择项则不显示检索框 diff --git a/zyplayer-doc-dubbo/pom.xml b/zyplayer-doc-dubbo/pom.xml index d5b94dad..6337cb2b 100644 --- a/zyplayer-doc-dubbo/pom.xml +++ b/zyplayer-doc-dubbo/pom.xml @@ -128,6 +128,8 @@ spring-context-support 1.0.2 + + diff --git a/zyplayer-doc-dubbo/src/main/java/com/zyplayer/doc/dubbo/controller/DubboController.java b/zyplayer-doc-dubbo/src/main/java/com/zyplayer/doc/dubbo/controller/DubboController.java index b3081cbe..e50687b8 100644 --- a/zyplayer-doc-dubbo/src/main/java/com/zyplayer/doc/dubbo/controller/DubboController.java +++ b/zyplayer-doc-dubbo/src/main/java/com/zyplayer/doc/dubbo/controller/DubboController.java @@ -26,8 +26,12 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; +import java.lang.reflect.Method; +import java.lang.reflect.Type; import java.net.URLDecoder; import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; /** @@ -84,11 +88,48 @@ public class DubboController { dubboNodeInfo.setIp(param.getIp()); dubboNodeInfo.setPort(param.getPort()); dubboNodeInfo.setInterfaceX(param.getService()); - String[] paramTypes = Optional.ofNullable(param.getParamTypes()).orElse(new String[]{}); - Object[] params = Optional.ofNullable(param.getParams()).orElse(new Object[]{}); + String paramTypeStr = Optional.ofNullable(param.getParamTypes()).orElse(""); + String paramsStr = Optional.ofNullable(param.getParams()).orElse(""); + List typeList = JSON.parseArray(paramTypeStr, String.class); + List paramList = JSON.parseArray(paramsStr, String.class); + List queryTypeList = new LinkedList<>(); + List queryParamList = new LinkedList<>(); + for (int i = 0; i < paramList.size(); i++) { + String typeStr = typeList.get(i); + String paramStr = paramList.get(i); + try { + if (typeStr.endsWith("[]")) { + String type = typeStr.substring(0, typeStr.length() - 2); + Class aClass = Class.forName(type); + List objects = JSON.parseArray(paramStr, aClass); + queryTypeList.add(typeStr); + queryParamList.add(objects); + } else if (typeStr.matches("java\\.util\\.List<.+>")) { + Pattern pattern = Pattern.compile("java\\.util\\.List<(.+)>"); + Matcher matcher = pattern.matcher(typeStr); + if (matcher.find()) { + String group = matcher.group(1); + Class aClass = Class.forName(group); + List objects = JSON.parseArray(paramStr, aClass); + queryParamList.add(objects); + queryTypeList.add("java.util.List"); + } + } else { + Class aClass = Class.forName(typeStr); + Object object = JSON.parseObject(paramStr, aClass); + queryParamList.add(object); + queryTypeList.add(typeStr); + } + } catch (Exception e) { + // 未找到对应类型,请手动引入项目包 + logger.error("未找到对应类型,请手动引入项目包:{}", typeStr); + queryParamList.add(paramStr); + queryTypeList.add(typeStr); + } + } GenericService bean = ReferenceConfigHolder.getBean(dubboNodeInfo); try { - Object result = bean.$invoke(param.getMethod(), paramTypes, params); + Object result = bean.$invoke(param.getMethod(), queryTypeList.toArray(new String[]{}), queryParamList.toArray()); return DocResponseJson.ok(result); } catch (Exception e) { e.printStackTrace(); @@ -120,6 +161,57 @@ public class DubboController { return DocResponseJson.ok(dubboInfoVo); } + /** + * 获取文档详情,依据类名生成 + * + * @author 暮光:城中城 + * @since 2019年2月10日 + **/ + @PostMapping(value = "/findDocInfo") + public DocResponseJson findDocInfo(DubboRequestParam param) { + List paramList = new LinkedList<>(); + try { + Class clazz = Class.forName(param.getService()); + Method[] methods = clazz.getMethods(); + for (Method method : methods) { + String methodName = method.getName(); + if (methodName.equals(param.getMethod())) { + Type[] parameterTypes = method.getGenericParameterTypes(); + for (Type clas : parameterTypes) { + DubboDocInfo.DubboDocParam docParam = new DubboDocInfo.DubboDocParam(); + docParam.setParamType(clas.getTypeName()); + paramList.add(docParam); + } + } + } + } catch (ClassNotFoundException e) { + return DocResponseJson.warn("未找到指定类,请引入相关包,类名:" + param.getService()); + } + if (paramList.isEmpty()) { + return DocResponseJson.ok(); + } + Map docInfoMap = new HashMap<>(); + String dubboServiceDoc = mgDubboStorageService.get(StorageKeys.DUBBO_SERVICE_DOC); + if (StringUtils.isNotBlank(dubboServiceDoc)) { + List docInfoList = JSON.parseArray(dubboServiceDoc, DubboDocInfo.class); + docInfoMap = docInfoList.stream().collect(Collectors.toMap(DubboDocInfo::getFunction, val -> val)); + } + String function = param.getService() + "." + param.getMethod(); + DubboDocInfo dubboDocInfo = docInfoMap.get(function); + if (dubboDocInfo == null) { + dubboDocInfo = new DubboDocInfo(); + dubboDocInfo.setParams(paramList); + dubboDocInfo.setFunction(function); + dubboDocInfo.setVersion(1); + dubboDocInfo.setService(param.getService()); + dubboDocInfo.setMethod(param.getMethod()); + docInfoMap.put(function, dubboDocInfo); + List docInfoList = new ArrayList<>(docInfoMap.values()); + mgDubboStorageService.put(StorageKeys.DUBBO_SERVICE_DOC, JSON.toJSONString(docInfoList)); + } + return DocResponseJson.ok(dubboDocInfo); + } + /** * 保存文档 * diff --git a/zyplayer-doc-dubbo/src/main/java/com/zyplayer/doc/dubbo/controller/param/DubboRequestParam.java b/zyplayer-doc-dubbo/src/main/java/com/zyplayer/doc/dubbo/controller/param/DubboRequestParam.java index 8eb5b6d9..424fb057 100644 --- a/zyplayer-doc-dubbo/src/main/java/com/zyplayer/doc/dubbo/controller/param/DubboRequestParam.java +++ b/zyplayer-doc-dubbo/src/main/java/com/zyplayer/doc/dubbo/controller/param/DubboRequestParam.java @@ -11,14 +11,14 @@ public class DubboRequestParam { private String method; private String ip; private Integer port; - private String[] paramTypes; - private Object[] params; + private String paramTypes; + private String params; - public String[] getParamTypes() { + public String getParamTypes() { return paramTypes; } - public void setParamTypes(String[] paramTypes) { + public void setParamTypes(String paramTypes) { this.paramTypes = paramTypes; } @@ -54,11 +54,11 @@ public class DubboRequestParam { this.port = port; } - public Object[] getParams() { + public String getParams() { return params; } - public void setParams(Object[] params) { + public void setParams(String params) { this.params = params; } } diff --git a/zyplayer-doc-dubbo/src/main/resources/doc-dubbo.html b/zyplayer-doc-dubbo/src/main/resources/doc-dubbo.html index b7736dca..52223d46 100644 --- a/zyplayer-doc-dubbo/src/main/resources/doc-dubbo.html +++ b/zyplayer-doc-dubbo/src/main/resources/doc-dubbo.html @@ -4,7 +4,7 @@ - + dubbo文档管理系统 @@ -51,7 +51,7 @@
{{dubboInfo.docInfo.explain}}编辑
- + 取消 保存
@@ -64,13 +64,13 @@ -
- -
1. 顺序必须和参数的顺序一致
2. 参数名意义不大,可不填
3. 类型必填,可手动输入,必须是全类名
- -
- 参数: -
+ + + + + + + @@ -81,11 +81,12 @@ - + + + + + +