提交 f5a40a4d authored 作者: 黄夏豪's avatar 黄夏豪

Merge remote-tracking branch 'origin/master'

package com.tykj.workflowcore.api.controller;
import com.tykj.workflowcore.api.annotations.Callable;
import com.tykj.workflowcore.api.annotations.CallableApi;
import com.tykj.workflowcore.api.entity.ApiInfo;
import com.tykj.workflowcore.api.entity.ClassInfo;
import com.tykj.workflowcore.api.entity.InvokeRequest;
import com.tykj.workflowcore.api.service.ApiService;
......@@ -23,14 +26,15 @@ public class ApiController {
@ApiOperation(value = "查询所有可调用函数信息")
@GetMapping
public ResponseEntity<List<ClassInfo>> findAll() {
public ResponseEntity<List<ApiInfo>> findAll() {
return ResponseEntity.ok(apiService.findAll());
}
@ApiOperation(value = "调用指定函数")
@PostMapping
public void invoke(@RequestBody InvokeRequest invokeRequest) {
apiService.invoke(invokeRequest.getClassName(),invokeRequest.getName(),invokeRequest.getParams());
apiService.invoke(invokeRequest.getClassName(), invokeRequest.getName(), invokeRequest.getParams());
}
}
......@@ -16,10 +16,16 @@ public class ApiInfo {
@ApiModelProperty(value = "所属类名")
private String className;
@ApiModelProperty(value = "方法名")
private String name;
@ApiModelProperty(value = "参数信息")
private List<EntityInfo> params;
@ApiModelProperty(value = "返回信息")
private EntityInfo ret;
@ApiModelProperty(value = "描述信息")
private String description;
}
......@@ -21,4 +21,6 @@ public class EntityInfo {
@ApiModelProperty(value = "变量信息")
private List<EntityInfo> fields;
@ApiModelProperty(value = "描述信息")
private String description;
}
......@@ -4,11 +4,13 @@ import com.alibaba.fastjson.JSON;
import com.tykj.workflowcore.api.annotations.Callable;
import com.tykj.workflowcore.api.annotations.CallableApi;
import com.tykj.workflowcore.api.entity.ApiInfo;
import com.tykj.workflowcore.api.entity.ClassInfo;
import com.tykj.workflowcore.api.entity.EntityInfo;
import com.tykj.workflowcore.api.entity.Parameter;
import com.tykj.workflowcore.base.util.ClassUtil;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import java.lang.reflect.Field;
......@@ -18,7 +20,7 @@ import java.util.*;
import java.util.stream.Collectors;
import static java.lang.String.format;
import static java.util.Objects.nonNull;
@Slf4j
@Service
......@@ -36,59 +38,68 @@ public class ApiService {
*
* @return 结构化列表数据,内容为带有指定注解的方法列表,也就是列出主项目中可调用的方法。
*/
public List<ClassInfo> findAll() {
public List<ApiInfo> findAll() {
//获取所有类
List<Class<?>> classes = ClassUtil.loadClassByLoader(getClass().getClassLoader());
return classes.stream()
.map(this::classInfo)
.flatMap(clz -> classInfo(clz).stream())
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
private ClassInfo classInfo(Class<?> clz) {
private List<ApiInfo> classInfo(Class<?> clz) {
try {
//确认Callable注解
if (clz.isAnnotationPresent(Callable.class)) {
List<ApiInfo> apiInfos = Arrays.stream(clz.getMethods())
.map(this::apiInfo)
return Arrays.stream(clz.getMethods())
.map(method -> apiInfo(clz.getName(), method))
.filter(Objects::nonNull)
.collect(Collectors.toList());
return new ClassInfo(clz.getName(), apiInfos);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
return new ArrayList<>();
}
private ApiInfo apiInfo(Method method) {
private ApiInfo apiInfo(String className, Method method) {
//确认CallableApi注解
if (method.isAnnotationPresent(CallableApi.class)) {
List<EntityInfo> params = Arrays.stream(method.getParameters())
.map(parameter -> entityInfo(parameter.getType(), parameter.getName()))
.map(parameter -> entityInfo(parameter.getType(), parameter.getName(), Strings.EMPTY))
.collect(Collectors.toList());
EntityInfo ret = entityInfo(method.getReturnType(), null);
return new ApiInfo(method.getName(), params, ret);
EntityInfo ret = entityInfo(method.getReturnType(), Strings.EMPTY, Strings.EMPTY);
ApiOperation methodAnnotation = method.getAnnotation(ApiOperation.class);
String description = Strings.EMPTY;
if (nonNull(methodAnnotation)) {
description = methodAnnotation.notes();
}
return new ApiInfo(className, method.getName(), params, ret, description);
} else {
return null;
}
}
private EntityInfo entityInfo(Class<?> clz, String name) {
String classType = clz.getName();
private EntityInfo entityInfo(Class<?> clz, String name, String description) {
String classType = clz.getCanonicalName();
List<EntityInfo> fields = new ArrayList<>();
if (isNotBasicClass(clz)) {
for (Field field : clz.getDeclaredFields()) {
Class<?> fieldClass = field.getType();
String fieldName = field.getName();
field.setAccessible(true);
//读取swagger描述信息
ApiModelProperty fieldAnnotation = field.getAnnotation(ApiModelProperty.class);
if (nonNull(fieldAnnotation)) {
description = fieldAnnotation.value();
}
//确认类中是否嵌套了相同类型的字段 防止死循环
boolean isDifferentClass = !Objects.equals(clz, fieldClass);
EntityInfo fieldClassInfo;
if (isDifferentClass) {
fieldClassInfo = entityInfo(fieldClass, fieldName);
fieldClassInfo = entityInfo(fieldClass, fieldName, description);
} else {
fieldClassInfo = new EntityInfo(fieldClass.getSimpleName(), field.getName(), new ArrayList<>());
fieldClassInfo = new EntityInfo(fieldClass.getSimpleName(), field.getName(), new ArrayList<>(), description);
}
fields.add(fieldClassInfo);
}
......@@ -96,7 +107,8 @@ public class ApiService {
return new EntityInfo(
classType,
name,
fields
fields,
description
);
}
......
......@@ -18,22 +18,31 @@ public class SpringBeanService implements ApplicationContextAware {
this.applicationContext = applicationContext;
}
}
//获取applicationContext
/**
* 获取applicationContext
*/
public ApplicationContext getApplicationContext() {
return this.applicationContext;
}
//通过name获取 Bean.
/**
* 通过name获取 Bean.
*/
public Object getBean(String name){
return getApplicationContext().getBean(name);
}
//通过class获取Bean.
/**
* 通过class获取Bean.
*/
public <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
}
//通过name,以及Clazz返回指定的Bean
/**
* 通过name,以及Clazz返回指定的Bean
*/
public <T> T getBean(String name,Class<T> clazz){
return getApplicationContext().getBean(name, clazz);
}
......
......@@ -128,7 +128,7 @@ public class ModelImpl implements ModelService {
List<ColumnVO> dataList = tableVO.getDataList();
TableInfo tableInfo = new TableInfo();
tableInfo.setName(tableVO.getModelName() + "_");
tableInfo.setName(tableVO.getModelName());
tableInfo.setCnName(tableVO.getModelTitle());
tableInfo.setXml(xmlMapping);
tableInfo.setModelType(tableVO.getModelType());
......@@ -249,7 +249,7 @@ public class ModelImpl implements ModelService {
TableVO tableVO = new TableVO();
String className = getClassName(aClass.toString());
//入表真实名称
String realName = className.toLowerCase() + "_model_test";
String realName = className.toLowerCase();
tableVO.setModelName(realName);
//获得类中文描述
if (aClass.isAnnotationPresent(ApiModel.class)) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论