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

Merge remote-tracking branch 'origin/master'

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