mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-09 04:59:21 +08:00
refactor: 优化部分代码
This commit is contained in:
@@ -56,6 +56,7 @@ import top.continew.starter.core.util.GeneralPropertySourceFactory;
|
|||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* API 文档自动配置
|
* API 文档自动配置
|
||||||
@@ -141,7 +142,7 @@ public class SpringDocAutoConfiguration implements WebMvcConfigurer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义 openapi 处理器
|
* 自定义 OpenApi 处理器
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public OpenAPIService openApiBuilder(Optional<OpenAPI> openAPI,
|
public OpenAPIService openApiBuilder(Optional<OpenAPI> openAPI,
|
||||||
@@ -155,54 +156,35 @@ public class SpringDocAutoConfiguration implements WebMvcConfigurer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 展示 枚举类型和值
|
* 自定义参数配置(针对 BaseEnum 展示枚举值和描述)
|
||||||
*
|
*
|
||||||
* @return
|
* @since 2.4.0
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public ParameterCustomizer customParameterCustomizer() {
|
public ParameterCustomizer customParameterCustomizer() {
|
||||||
return (parameterModel, methodParameter) -> {
|
return (parameterModel, methodParameter) -> {
|
||||||
// 判断方法参数类型是否为 IBaseEnum 的子类型
|
Class<?> parameterType = methodParameter.getParameterType();
|
||||||
if (ClassUtil.isAssignable(BaseEnum.class, methodParameter.getParameterType())) {
|
// 判断是否为 BaseEnum 的子类型
|
||||||
String description = parameterModel.getDescription();
|
if (!ClassUtil.isAssignable(BaseEnum.class, parameterType)) {
|
||||||
// TODO 会重复调用,有什么优雅的判读方式吗?
|
return parameterModel;
|
||||||
if (StrUtil.contains(description, "color:red")) {
|
|
||||||
return parameterModel;
|
|
||||||
}
|
|
||||||
Schema schema = parameterModel.getSchema();
|
|
||||||
|
|
||||||
// 获取方法参数类型的所有枚举常量
|
|
||||||
BaseEnum[] enumConstants = (BaseEnum[])methodParameter.getParameterType().getEnumConstants();
|
|
||||||
List<String> list = new ArrayList<>();
|
|
||||||
Map<Object, String> descMap = new HashMap<>();
|
|
||||||
|
|
||||||
// 遍历枚举常量,获取其值和描述
|
|
||||||
for (BaseEnum constant : enumConstants) {
|
|
||||||
list.add(constant.getValue().toString());
|
|
||||||
descMap.put(constant.getValue(), constant.getDescription());
|
|
||||||
}
|
|
||||||
|
|
||||||
// 枚举值类型
|
|
||||||
String enumValueType = EnumTypeUtils.getEnumValueTypeAsString(methodParameter.getParameterType());
|
|
||||||
schema.setType(enumValueType);
|
|
||||||
switch (enumValueType) {
|
|
||||||
case "integer" -> schema.setFormat("int32");
|
|
||||||
case "long" -> schema.setFormat("int64");
|
|
||||||
case "number" -> schema.setFormat("double");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置枚举值列表和描述
|
|
||||||
schema.setEnum(list);
|
|
||||||
parameterModel.setDescription(description + "<span style='color:red'>" + descMap + "</span>");
|
|
||||||
}
|
}
|
||||||
|
String description = parameterModel.getDescription();
|
||||||
|
if (StrUtil.contains(description, "color:red")) {
|
||||||
|
return parameterModel;
|
||||||
|
}
|
||||||
|
// 封装参数配置
|
||||||
|
this.configureSchema(parameterModel.getSchema(), parameterType);
|
||||||
|
// 自定义枚举描述
|
||||||
|
parameterModel.setDescription(description + "<span style='color:red'>" + this
|
||||||
|
.getDescMap(parameterType) + "</span>");
|
||||||
return parameterModel;
|
return parameterModel;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 展示 枚举类型和值
|
* 自定义参数配置(针对 BaseEnum 展示枚举值和描述)
|
||||||
*
|
*
|
||||||
* @return
|
* @since 2.4.0
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public PropertyCustomizer customPropertyCustomizer() {
|
public PropertyCustomizer customPropertyCustomizer() {
|
||||||
@@ -216,36 +198,55 @@ public class SpringDocAutoConfiguration implements WebMvcConfigurer {
|
|||||||
} else {
|
} else {
|
||||||
rawClass = Object.class;
|
rawClass = Object.class;
|
||||||
}
|
}
|
||||||
|
// 判断是否为 BaseEnum 的子类型
|
||||||
// 检查原始类是否实现了 IBaseEnum 接口
|
if (!ClassUtil.isAssignable(BaseEnum.class, rawClass)) {
|
||||||
if (ClassUtil.isAssignable(BaseEnum.class, rawClass)) {
|
|
||||||
BaseEnum[] enumConstants = (BaseEnum[])rawClass.getEnumConstants();
|
|
||||||
List<String> list = new ArrayList<>();
|
|
||||||
Map<Object, String> descMap = new HashMap<>();
|
|
||||||
// 遍历枚举常量,获取其值和描述
|
|
||||||
for (BaseEnum constant : enumConstants) {
|
|
||||||
list.add(constant.getValue().toString());
|
|
||||||
descMap.put(constant.getValue(), constant.getDescription());
|
|
||||||
}
|
|
||||||
// 获取泛型类型
|
|
||||||
String enumValueType = EnumTypeUtils.getEnumValueTypeAsString(rawClass);
|
|
||||||
schema.setType(enumValueType);
|
|
||||||
// 根据枚举值类型设置 schema 的格式
|
|
||||||
switch (enumValueType) {
|
|
||||||
case "integer" -> schema.setFormat("int32");
|
|
||||||
case "long" -> schema.setFormat("int64");
|
|
||||||
case "number" -> schema.setFormat("double");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置枚举值列表和描述
|
|
||||||
schema.setEnum(list);
|
|
||||||
schema.setDescription(schema.getDescription() + "<span style='color:red'>" + descMap + "</span>");
|
|
||||||
return schema;
|
return schema;
|
||||||
}
|
}
|
||||||
|
// 封装参数配置
|
||||||
|
this.configureSchema(schema, rawClass);
|
||||||
|
// 自定义参数描述
|
||||||
|
schema.setDescription(schema.getDescription() + "<span style='color:red'>" + this
|
||||||
|
.getDescMap(rawClass) + "</span>");
|
||||||
return schema;
|
return schema;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 封装 Schema 配置
|
||||||
|
*
|
||||||
|
* @param schema Schema
|
||||||
|
* @param enumClass 枚举类型
|
||||||
|
* @since 2.4.0
|
||||||
|
*/
|
||||||
|
private void configureSchema(Schema schema, Class<?> enumClass) {
|
||||||
|
BaseEnum[] enums = (BaseEnum[])enumClass.getEnumConstants();
|
||||||
|
// 设置枚举可用值
|
||||||
|
List<String> valueList = Arrays.stream(enums).map(e -> e.getValue().toString()).toList();
|
||||||
|
schema.setEnum(valueList);
|
||||||
|
// 设置枚举值类型和格式
|
||||||
|
String enumValueType = EnumTypeUtils.getEnumValueTypeAsString(enumClass);
|
||||||
|
schema.setType(enumValueType);
|
||||||
|
switch (enumValueType) {
|
||||||
|
case "integer" -> schema.setFormat("int32");
|
||||||
|
case "long" -> schema.setFormat("int64");
|
||||||
|
case "number" -> schema.setFormat("double");
|
||||||
|
default -> schema.setFormat(enumValueType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取枚举描述 Map
|
||||||
|
*
|
||||||
|
* @param enumClass 枚举类型
|
||||||
|
* @return 枚举描述 Map
|
||||||
|
* @since 2.4.0
|
||||||
|
*/
|
||||||
|
private Map<Object, String> getDescMap(Class<?> enumClass) {
|
||||||
|
BaseEnum[] enums = (BaseEnum[])enumClass.getEnumConstants();
|
||||||
|
return Arrays.stream(enums)
|
||||||
|
.collect(Collectors.toMap(BaseEnum::getValue, BaseEnum::getDescription, (a, b) -> a, LinkedHashMap::new));
|
||||||
|
}
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void postConstruct() {
|
public void postConstruct() {
|
||||||
log.debug("[ContiNew Starter] - Auto Configuration 'ApiDoc' completed initialization.");
|
log.debug("[ContiNew Starter] - Auto Configuration 'ApiDoc' completed initialization.");
|
||||||
|
@@ -47,7 +47,10 @@ import java.util.stream.Collectors;
|
|||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 自定义 openapi 处理器 对源码功能进行修改 增强使用
|
* 自定义 OpenApi 处理器(对源码功能进行修改,增强使用)
|
||||||
|
*
|
||||||
|
* @author echo
|
||||||
|
* @since 2.4.0
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
public class OpenApiHandler extends OpenAPIService {
|
public class OpenApiHandler extends OpenAPIService {
|
||||||
|
@@ -24,16 +24,19 @@ import java.lang.reflect.Type;
|
|||||||
/**
|
/**
|
||||||
* 枚举类型工具
|
* 枚举类型工具
|
||||||
*
|
*
|
||||||
* @Author echo
|
* @author echo
|
||||||
* @date 2024/07/31
|
* @since 2.4.0
|
||||||
*/
|
*/
|
||||||
public class EnumTypeUtils {
|
public class EnumTypeUtils {
|
||||||
|
|
||||||
|
private EnumTypeUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取enum值类型
|
* 获取枚举值类型
|
||||||
*
|
*
|
||||||
* @param enumClass enum class
|
* @param enumClass 枚举类型
|
||||||
* @return {@link String }
|
* @return 枚举值类型
|
||||||
*/
|
*/
|
||||||
public static String getEnumValueTypeAsString(Class<?> enumClass) {
|
public static String getEnumValueTypeAsString(Class<?> enumClass) {
|
||||||
try {
|
try {
|
||||||
@@ -44,25 +47,26 @@ public class EnumTypeUtils {
|
|||||||
// 检查接口是否为参数化类型
|
// 检查接口是否为参数化类型
|
||||||
if (type instanceof ParameterizedType parameterizedType) {
|
if (type instanceof ParameterizedType parameterizedType) {
|
||||||
// 检查接口的原始类型是否为 BaseEnum
|
// 检查接口的原始类型是否为 BaseEnum
|
||||||
if (parameterizedType.getRawType() == BaseEnum.class) {
|
if (parameterizedType.getRawType() != BaseEnum.class) {
|
||||||
Type actualType = parameterizedType.getActualTypeArguments()[0];
|
continue;
|
||||||
// 检查实际类型参数是否为类类型
|
}
|
||||||
if (actualType instanceof Class<?> actualClass) {
|
Type actualType = parameterizedType.getActualTypeArguments()[0];
|
||||||
if (actualClass == Integer.class) {
|
// 检查实际类型参数是否为类类型
|
||||||
return "integer";
|
if (actualType instanceof Class<?> actualClass) {
|
||||||
} else if (actualClass == Long.class) {
|
if (actualClass == Integer.class) {
|
||||||
return "long";
|
return "integer";
|
||||||
} else if (actualClass == Double.class) {
|
} else if (actualClass == Long.class) {
|
||||||
return "number";
|
return "long";
|
||||||
} else if (actualClass == String.class) {
|
} else if (actualClass == Double.class) {
|
||||||
return "string";
|
return "number";
|
||||||
}
|
} else if (actualClass == String.class) {
|
||||||
|
return "string";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception ignored) {
|
||||||
e.printStackTrace();
|
// ignored
|
||||||
}
|
}
|
||||||
return "string";
|
return "string";
|
||||||
}
|
}
|
||||||
|
@@ -77,9 +77,11 @@ public class MybatisPlusAutoConfiguration {
|
|||||||
public MybatisPlusInterceptor mybatisPlusInterceptor(MyBatisPlusExtensionProperties properties) {
|
public MybatisPlusInterceptor mybatisPlusInterceptor(MyBatisPlusExtensionProperties properties) {
|
||||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||||
// 数据权限插件
|
// 数据权限插件
|
||||||
MyBatisPlusExtensionProperties.DataPermissionProperties dataPermissionProperties = properties.getDataPermission();
|
MyBatisPlusExtensionProperties.DataPermissionProperties dataPermissionProperties = properties
|
||||||
|
.getDataPermission();
|
||||||
if (null != dataPermissionProperties && dataPermissionProperties.isEnabled()) {
|
if (null != dataPermissionProperties && dataPermissionProperties.isEnabled()) {
|
||||||
interceptor.addInnerInterceptor(new DataPermissionInterceptor(SpringUtil.getBean(DataPermissionHandler.class)));
|
interceptor.addInnerInterceptor(new DataPermissionInterceptor(SpringUtil
|
||||||
|
.getBean(DataPermissionHandler.class)));
|
||||||
}
|
}
|
||||||
// 分页插件
|
// 分页插件
|
||||||
MyBatisPlusExtensionProperties.PaginationProperties paginationProperties = properties.getPagination();
|
MyBatisPlusExtensionProperties.PaginationProperties paginationProperties = properties.getPagination();
|
||||||
|
@@ -73,7 +73,6 @@
|
|||||||
<flatten.version>1.6.0</flatten.version>
|
<flatten.version>1.6.0</flatten.version>
|
||||||
<spotless.version>2.43.0</spotless.version>
|
<spotless.version>2.43.0</spotless.version>
|
||||||
<sonar.version>3.11.0.3922</sonar.version>
|
<sonar.version>3.11.0.3922</sonar.version>
|
||||||
<maven-compiler-plugin.verison>3.11.0</maven-compiler-plugin.verison>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
@@ -655,12 +654,6 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
<pluginManagement>
|
<pluginManagement>
|
||||||
<plugins>
|
<plugins>
|
||||||
<!-- 编译插件 -->
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
|
||||||
<version>${maven-compiler-plugin.verison}</version>
|
|
||||||
</plugin>
|
|
||||||
<!-- 统一版本号插件 -->
|
<!-- 统一版本号插件 -->
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.codehaus.mojo</groupId>
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
Reference in New Issue
Block a user