Skip to content

Commit 7680674

Browse files
committed
feat: 添加接口接收枚举类的通用转换器
1 parent 48e620d commit 7680674

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package top.cadecode.sra.framework.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.format.FormatterRegistry;
5+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
6+
import top.cadecode.sra.framework.convert.EnumConvertorFactory;
7+
8+
/**
9+
* @author Cade Li
10+
* @date 2022/6/14
11+
* @description Spring MVC 配置
12+
*/
13+
@Configuration
14+
public class WebMvcConfig implements WebMvcConfigurer {
15+
16+
@Override
17+
public void addFormatters(FormatterRegistry registry) {
18+
// 添加枚举类的转换器工厂
19+
registry.addConverterFactory(new EnumConvertorFactory());
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package top.cadecode.sra.framework.convert;
2+
3+
/**
4+
* @author Cade Li
5+
* @date 2022/5/28
6+
* @description 请求参数枚举转换接口
7+
*/
8+
public interface EnumConvertor {
9+
10+
/**
11+
* 返回枚举元素的对应标记
12+
*/
13+
String convertBy();
14+
15+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package top.cadecode.sra.framework.convert;
2+
3+
import org.springframework.core.convert.converter.Converter;
4+
import org.springframework.core.convert.converter.ConverterFactory;
5+
6+
import java.util.Arrays;
7+
import java.util.Map;
8+
import java.util.Objects;
9+
import java.util.concurrent.ConcurrentHashMap;
10+
11+
import static java.util.stream.Collectors.toMap;
12+
13+
/**
14+
* @author Cade Li
15+
* @date 2022/6/14
16+
* @description 字符串转枚举转换器工厂
17+
*/
18+
public class EnumConvertorFactory implements ConverterFactory<String, EnumConvertor> {
19+
20+
private static final Map<Class<?>, Converter<String, ?>> CONVERTER_MAP = new ConcurrentHashMap<>();
21+
22+
@SuppressWarnings("unchecked")
23+
@Override
24+
public <T extends EnumConvertor> Converter<String, T> getConverter(Class<T> targetType) {
25+
Converter<String, ?> stringConverter = CONVERTER_MAP.get(targetType);
26+
if (Objects.isNull(stringConverter)) {
27+
stringConverter = new StringEnumConvertor<>(targetType);
28+
CONVERTER_MAP.put(targetType, stringConverter);
29+
}
30+
return (Converter<String, T>) stringConverter;
31+
}
32+
33+
/**
34+
* 字符串转枚举转换器
35+
*/
36+
public static class StringEnumConvertor<T extends EnumConvertor> implements Converter<String, T> {
37+
38+
private final Map<String, T> enumMap;
39+
40+
public StringEnumConvertor(Class<T> targetType) {
41+
enumMap = Arrays.stream(targetType.getEnumConstants())
42+
.collect(toMap(EnumConvertor::convertBy, o -> o, (p, n) -> n));
43+
}
44+
45+
@Override
46+
public T convert(String source) {
47+
return enumMap.get(source);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)