測(cè)試
首先先建立一個(gè)枚舉類:
public enum ScoreType {
TOTAL_SCORE("總評(píng)成績(jī)"),
MIDDLE_SCORE("期中成績(jī)"),
FINAL_SCORE("期末成績(jī)");
String des; // 描述
ScoreType(String des) {
this.des = des;
}
public String getDes() {
return des;
}
}
再建立一個(gè)枚舉api接口:
@RestController
@RequestMapping("/Klass")
public class KlassController {
@GetMapping("testEnum")
public String testEnum(@RequestParam ScoreType scoreType) {
return "枚舉序號(hào):" + scoreType.ordinal() + ",枚舉名:" + scoreType.name();
}
}
進(jìn)行測(cè)試,使用枚舉名發(fā)送數(shù)據(jù):
使用枚舉序號(hào)發(fā)送數(shù)據(jù):
由此可見,在springboot默認(rèn)請(qǐng)求參數(shù)映射中,枚舉類型只能通過枚舉名來進(jìn)行參數(shù)映射,但有時(shí)候我們需要用序號(hào)來做映射。
Converter
顧明思議Converter就是轉(zhuǎn)換的意思,我們可以通過定義的Converter來確定參數(shù)到枚舉類型之間的轉(zhuǎn)換:
public class BaseEnumConverter<T extends Enum> implements Converter<String, T> {
private Map<String, T> enumMap = new HashMap<>();
public BaseEnumConverter(Class<T> enumType) {
T[] enums = enumType.getEnumConstants();
for (T e : enums) {
enumMap.put(String.valueOf(e.ordinal()), e);
enumMap.put(e.name(), e);
}
}
@Override
public T convert(String source) {
T t1 = enumMap.get(source.toLowerCase());
T t2 = enumMap.get(source.toUpperCase());
if (t1 == null && t2 == null) {
throw new IllegalArgumentException("無法匹配對(duì)應(yīng)的枚舉類型");
}
return t1 == null ? t2 : t1;
}
}
分析代碼,根據(jù)運(yùn)行時(shí)具體枚舉類的參數(shù),獲取所有枚舉值,并將各個(gè)枚舉值序列和枚舉值名與枚舉值之間做映射(保存在Map中),如上述枚舉類型,將會(huì)生成以下Map:
0 => ScoreType.TOTAL_SCORE TOTAL_SCORE => ScoreType.TOTAL_SCORE 1 => ScoreType.MIDDLE_SCORE MIDDLE_SCORE => ScoreType.MIDDLE_SCORE 2 => ScoreType.FINAL_SCORE FINAL_SCORE => ScoreType.FINAL_SCORE
通過此Converter,就可以實(shí)現(xiàn)前臺(tái)傳序號(hào)和枚舉名,都能成功映射到枚舉類型,將此Converter通過工廠模式提供到springboot中:
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(Class<T> targetType) {
// 每一個(gè)類型創(chuàng)建一個(gè)轉(zhuǎn)換器
Converter<String, T> converter = CONVERTERS.get(targetType);
if (converter == null) {
converter = new BaseEnumConverter<>(targetType);
CONVERTERS.put(targetType, converter);
}
return converter;
}
}
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverterFactory(new BaseEnumConverterFactory());
}
}
進(jìn)行測(cè)試,使用枚舉名發(fā)送數(shù)據(jù):
靈活化
為了保證靈活性,每個(gè)枚舉類型可以自定義轉(zhuǎn)換的方式,建立一個(gè)接口,對(duì)接口進(jìn)行類型轉(zhuǎn)換,建立一個(gè)BaseEnum接口:
public interface BaseEnum {
String[] getKeys(); //返回的keys可轉(zhuǎn)換為BaseEnum
}
枚舉類實(shí)現(xiàn)此接口,并定義映射方式
public enum ScoreType implements BaseEnum {
TOTAL_SCORE("總評(píng)成績(jī)"),
MIDDLE_SCORE("期中成績(jī)"),
FINAL_SCORE("期末成績(jī)");
String des; // 描述
ScoreType(String des) {
this.des = des;
}
public String getDes() {
return des;
}
@Override
public String[] getKeys() {
String[] s = {String.valueOf(this.ordinal()), this.name()}; // 次序和名字都可轉(zhuǎn)為枚舉,如0和total_score => ScoreType.TOTAL_SCORE
return s;
}
}
轉(zhuǎn)換器統(tǒng)一對(duì)BaseEnum進(jìn)行轉(zhuǎn)換:
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();
// 根據(jù)keys建立轉(zhuǎn)換
for (T e : enums) {
for (String key : e.getKeys()) {
enumMap.put(key, e);
}
}
}
@Override
public T convert(String source) {
T t1 = enumMap.get(source.toLowerCase());
T t2 = enumMap.get(source.toUpperCase());
if (t1 == null && t2 == null) {
throw new IllegalArgumentException("無法匹配對(duì)應(yīng)的枚舉類型");
}
return t1 == null ? t2 : t1;
}
}
對(duì)于每個(gè)枚舉類型,可通過返回的keys來自定義轉(zhuǎn)換的方式。
以上就是關(guān)于 springboot 是怎么一步一步實(shí)現(xiàn)枚舉類型傳遞的過程的詳細(xì)內(nèi)容,想要了解更多關(guān)于 springboot 枚舉類型和 springboot 框架其他方面的知識(shí),請(qǐng)關(guān)注W3Cschool其它相關(guān)文章!如果本篇文章對(duì)大家的學(xué)習(xí)有所幫助,還希望大家能夠多多地支持我們!