通用枚举
1 2 3 4 5 6 7 8 9
| @FunctionalInterface public interface BaseEnum {
String[] getKeys(); }
|
枚举类实现此接口,并定义映射方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package com.yandi.web.core.config;
public enum DocxEnum implements BaseEnum {
SERVICE_PROTECTION("服务保障"),
ITINERARY("行程安排"),
ACCOMMODATION("住宿安排"), ;
private final String title;
DocxEnum(String title) { this.title = title; }
public String getTitle() { return this.title; }
@Override public String[] getKeys() { return new String[] {String.valueOf(this.ordinal()), this.name().toLowerCase()}; } }
|
转换器统一对 BaseEnum 进行转换,对于每个枚举类型,可通过返回的 keys 来自定义转换的方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.yandi.web.core.config;
import org.springframework.core.convert.converter.Converter;
import java.util.HashMap; import java.util.Map;
public class BaseEnumConverter<T extends BaseEnum> implements Converter<String, T> { private Map<String, T> enumMap = new HashMap<>(); public BaseEnumConverter(Class<T> enumType) { T[] enums = enumType.getEnumConstants(); for (T e : enums) { for (String key : e.getKeys()) { enumMap.put(key, e); } } }
@Override public T convert(String source) { T t = enumMap.get(source.toLowerCase()); if (t == null) { throw new IllegalArgumentException("无法匹配对应的枚举类型"); } return t; } }
|
构造转换器工厂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.yandi.web.core.config;
import org.jetbrains.annotations.NotNull; import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.ConverterFactory;
import java.util.HashMap; import java.util.Map;
public class BaseEnumConverterFactory implements ConverterFactory<String, Enum> { private static final Map<Class, Converter> CONVERTERS = new HashMap<>(); @Override public <T extends Enum> Converter<String, T> getConverter(@NotNull Class<T> targetType) { Converter<String, T> converter = CONVERTERS.get(targetType); if (converter == null) { converter = new BaseEnumConverter(targetType); CONVERTERS.put(targetType, converter); } return converter; } }
|
将此 Converter 通过工厂模式提供到 springboot 中:
1 2 3 4 5 6 7 8
| @Configuration @EnableWebMvc public class WebConfig implements WebMvcConfigurer { @Override public void addFormatters(FormatterRegistry registry) { registry.addConverterFactory(new BaseEnumConverterFactory()); } }
|
开始使用。
注意这里的 DocxEnum 用法。
1 2 3 4 5
| @PostMapping("/arrangement") public Object importData(final MultipartFile[] file, @RequestParam(name = "type") final DocxEnum docxEnum) throws IOException { ... }
|