dubbo参数自动获取
This commit is contained in:
@@ -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<String> typeList = JSON.parseArray(paramTypeStr, String.class);
|
||||
List<String> paramList = JSON.parseArray(paramsStr, String.class);
|
||||
List<String> queryTypeList = new LinkedList<>();
|
||||
List<Object> 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<DubboDocInfo.DubboDocParam> 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<String, DubboDocInfo> docInfoMap = new HashMap<>();
|
||||
String dubboServiceDoc = mgDubboStorageService.get(StorageKeys.DUBBO_SERVICE_DOC);
|
||||
if (StringUtils.isNotBlank(dubboServiceDoc)) {
|
||||
List<DubboDocInfo> 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<DubboDocInfo> docInfoList = new ArrayList<>(docInfoMap.values());
|
||||
mgDubboStorageService.put(StorageKeys.DUBBO_SERVICE_DOC, JSON.toJSONString(docInfoList));
|
||||
}
|
||||
return DocResponseJson.ok(dubboDocInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存文档
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user