diff --git a/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/mvc/CustomEnumGenericConverter.java b/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/mvc/CustomEnumGenericConverter.java new file mode 100644 index 000000000..04afe69c6 --- /dev/null +++ b/kernel-d-wrapper/wrapper-field-sdk/src/main/java/cn/stylefeng/roses/kernel/wrapper/field/mvc/CustomEnumGenericConverter.java @@ -0,0 +1,71 @@ +package cn.stylefeng.roses.kernel.wrapper.field.mvc; + +import cn.stylefeng.roses.kernel.rule.base.ReadableEnum; +import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.converter.GenericConverter; + +import java.util.HashSet; +import java.util.Set; + +/** + * 自定义枚举转化器 + *

+ * 将string,int,boolean 转化为 枚举类型 + *

+ * 枚举类型必须是实现 {@link ReadableEnum}接口的 + * + * @author fengshuonan + * @date 2022/9/24 18:31 + */ +public class CustomEnumGenericConverter implements GenericConverter { + + /** + * 获取可支持的转化类型 + * + * @author fengshuonan + * @date 2022/9/24 18:34 + */ + @Override + public Set getConvertibleTypes() { + + Set convertiblePairs = new HashSet<>(); + + // 这里我们可以定义多组的类型转换关系 + ConvertiblePair stringPair = new ConvertiblePair(String.class, ReadableEnum.class); + ConvertiblePair intPair = new ConvertiblePair(Integer.class, ReadableEnum.class); + ConvertiblePair booleanPair = new ConvertiblePair(Boolean.class, ReadableEnum.class); + + convertiblePairs.add(stringPair); + convertiblePairs.add(intPair); + convertiblePairs.add(booleanPair); + + return convertiblePairs; + } + + /** + * 具体转化过程 + * + * @param source 源数值 + * @param sourceType 源类型 + * @param targetType 目标类型 + * @return 被转化后的值 + * @author fengshuonan + * @date 2022/9/24 18:34 + */ + @Override + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { + + // 如果目标是枚举并且是实现了ReadableEnum接口的 + if (ReadableEnum.class.isAssignableFrom(targetType.getObjectType())) { + if (targetType.getObjectType().isEnum()) { + ReadableEnum[] enumConstants = (ReadableEnum[]) targetType.getObjectType().getEnumConstants(); + for (ReadableEnum enumConstant : enumConstants) { + return enumConstant.parseToEnum(String.valueOf(source)); + } + } + } + + return source; + } + +} \ No newline at end of file diff --git a/kernel-d-wrapper/wrapper-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/wrapper/starter/GunsWrapperAutoConfiguration.java b/kernel-d-wrapper/wrapper-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/wrapper/starter/GunsWrapperAutoConfiguration.java index 626d7f453..a48cdb1f9 100644 --- a/kernel-d-wrapper/wrapper-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/wrapper/starter/GunsWrapperAutoConfiguration.java +++ b/kernel-d-wrapper/wrapper-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/wrapper/starter/GunsWrapperAutoConfiguration.java @@ -25,6 +25,7 @@ package cn.stylefeng.roses.kernel.wrapper.starter; import cn.stylefeng.roses.kernel.wrapper.WrapperAop; +import cn.stylefeng.roses.kernel.wrapper.field.mvc.CustomEnumGenericConverter; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -50,4 +51,15 @@ public class GunsWrapperAutoConfiguration { return new WrapperAop(); } + /** + * 自定义spring mvc的参数转化器,将请求参数转化为枚举 + * + * @author fengshuonan + * @date 2022/9/24 18:32 + */ + @Bean + public CustomEnumGenericConverter customEnumGenericConverter() { + return new CustomEnumGenericConverter(); + } + }