SpringBoot conversion parameter passing enumeration

Sometimes we pass parameters when you want to use as a parameter enumeration class

public enum VipEnum {
HUANG(1, "黄钻"),
HONG(2, "红钻");
private Integer id;
private String value;

VipEnum(Integer id, String value) {
this.id = id;
this.value = value;
}

public Integer getId() {
return id;
}

String the getValue public () {
return value;
}
}
1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
we expect is such a background pass a parameter is automatically converted to enumeration class VipEnum.HUANG

@PostMapping("/vip")
public VipEnum convert(VipEnum vipEnum) {
return vipEnum;
}

1
2
3
4
5
implement the Converter interface
at this time how to do it? We begin the analysis, is not the type of converter used in spring it? provides us with a spring Auto Switch Interface Converter <S, T>, can be realized from one into another Object Object function. In addition to the Interface Converter

Converter interface public <S, T> {
T Convert (Source S);
}
. 1
2
. 3
And then we implemented a little bit

@Component
@Slf4j
public class PersonConverter implements Converter<String, VipEnum> {

@Override
public VipEnum convert(String value) {
log.info("参数是: {}", value);
return (VipEnum) PersonConverter.getEnum(VipEnum.class, value);
}


public static <T extends VipEnum> Object getEnum(Class<T> targerType, String source) {
for (T enumObj : targerType.getEnumConstants()) {
if (source.equals(String.valueOf(enumObj.getId()))) {
return enumObj;
}
}
return null;
}

}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
so that it can achieve a conversion, it is noted in ah Spring MVC and Spring Boot, since the request received from the client have been regarded as a String, you can only turn the String enumeration converter.

ConverterFactory interfaces
ConverterFactory emergence allows us to unified management of some of the associated Converter. As the name suggests, it is to produce a factory ConverterFactory Converter indeed ConverterFactory is used to generate the Converter. Let's look at the definition of the interface ConverterFactory

interface ConverterFactory public <S, R & lt> {
<R & lt T the extends> Converter <S, T> GetConverter (Class <T> the targetType);
}
. 1
2
. 3
can be seen that a total of three generic S, R, T, wherein

It indicates the type of the original S
R represents the target type
T R is the type of a subclass
ConverterFactory wherein ConverterFactory original type can be converted into a set of objects that implement the same interface type of benefits compared to the converter, and the Converter can convert one type into a total not an enumeration to achieve a Converter, ah, this is certainly not acceptable. Once you have ConverterFactory, things become a lot simpler!

First, get hold of the base class interface

interface IEnum public <T> {
T getId ();
}
. 1
2
. 3
and it allows enumeration class implements

public enum VipEnum implements IEnum<Integer> {
HUANG(1, "黄钻"),
HONG(2, "红钻");
private Integer id;
private String value;

VipEnum(Integer id, String value) {
this.id = id;
this.value = value;
}

@Override
public Integer getId() {
return id;
}

String the getValue public () {
return value;
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
then converted to create a factory class enumeration

@Component
public class EnumConvertFactory implements ConverterFactory<String, IEnum> {
@Override
public <T extends IEnum> Converter<String, T> getConverter(Class<T> targetType) {
return new StringToIEum<>(targetType);
}

@SuppressWarnings("all")
private static class StringToIEum<T extends IEnum> implements Converter<String, T> {
private Class<T> targerType;
public StringToIEum(Class<T> targerType) {
this.targerType = targerType;
}

@Override
public T convert(String source) {
if (StringUtils.isEmpty(source)) {
return null;
}
return (T) EnumConvertFactory.getIEnum(this.targerType, source);
}
}

public static <T extends IEnum> Object getIEnum(Class<T> targerType, String source) {
for (T enumObj : targerType.getEnumConstants()) {
if (source.equals(String.valueOf(enumObj.getId()))) {
return enumObj;
}
}
return null;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
this is not enough, followed required injection spring containers converted plant, I use it here is springbooot2.x

@Configuration
public class WebConfig implements WebMvcConfigurer {

@Autowired
private EnumConvertFactory enumConvertFactory;

@Override
public void addFormatters (FormatterRegistry Registry) {
registry.addConverterFactory (enumConvertFactory);
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
then can be converted, to be converted enumeration base interface can implement it It is not the Bang Bang! !

Guess you like

Origin www.cnblogs.com/hyhy904/p/10962037.html