如何在spring boot項(xiàng)目中使用枚舉?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
成都創(chuàng)新互聯(lián)主要從事做網(wǎng)站、成都網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)瀘縣,10年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):13518219792
1. 在controller的方法中,比如以這個(gè)枚舉為參數(shù),如下代碼:
@RequestMapping("/**") public String getRejectReasons(EnumExpenseType type) { return ""; }
前臺(tái)傳入的參數(shù)如果是type:1, 那它值應(yīng)該是:小歡喜,實(shí)際上呢?
Caused by: java.lang.IllegalArgumentException: No enum constant com.**.EnumReasonType.1 at java.lang.Enum.valueOf(Enum.java:238) ~[?:1.8.0_111] at org.springframework.core.convert.support.StringToEnumConverterFactory$StringToEnum.convert(StringToEnumConverterFactory.java:52) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.core.convert.support.StringToEnumConverterFactory$StringToEnum.convert(StringToEnumConverterFactory.java:38) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.core.convert.support.GenericConversionService$ConverterFactoryAdapter.convert(GenericConversionService.java:436) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191) ~[spring-core-5.1.8.RELEASE.jar:5.1.8.RELEASE] at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:129) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:73) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:53) ~[spring-beans-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:693) ~[spring-context-5.1.7.RELEASE.jar:5.1.7.RELEASE] at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:124) ~[spring-web-5.1.7.RELEASE.jar:5.1.7.RELEASE] ... 81 more
Failed to convert value of type 'java.lang.String' to required type 'com.**.EnumExpenseType';
nested exception is org.springframework.core.convert.ConversionFailedException:
Failed to convert from type [java.lang.String] to type [com.**.EnumExpenseType] for value '1';
nested exception is java.lang.IllegalArgumentException: No enum constant com.***.EnumExpenseType.1
實(shí)際上它卻報(bào)了個(gè)錯(cuò)。轉(zhuǎn)換失敗了。
查看報(bào)錯(cuò)信息,可以定位到是spring框架中StringToEnumConverterFactory中轉(zhuǎn)換失敗,具體代碼如下:
private static class StringToEnumimplements Converter { private final Class enumType; public StringToEnum(Class enumType) { this.enumType = enumType; } @Override public T convert(String source) { if (source.isEmpty()) { // It's an empty enum identifier: reset the enum value to null. return null; } return (T) Enum.valueOf(this.enumType, source.trim()); } }
是Enum.valueOf這里報(bào)錯(cuò),Enum.valueOf的后面的值并不是我們的value,而是name(這里的小歡喜)。
所以,我們不能使用這個(gè)spring提供converter,需要自定義一個(gè):StringToEnumConverterFactory
public class StringToEnumConverterFactory implements ConverterFactory{ private static final Map converterMap = new HashMap<>(); @Override public Converter getConverter(Class targetType) { Converter converter = converterMap.get(targetType); if(converter == null) { converter = new StringToEnumConverter<>(targetType); converterMap.put(targetType, converter); } return converter; } class StringToEnumConverter implements Converter { private Map enumMap = new HashMap<>(); StringToEnumConverter(Class enumType) { T[] enums = enumType.getEnumConstants(); for(T e : enums) { enumMap.put(String.valueOf(e.getValue()), e); } } @Override public T convert(String source) { T t = enumMap.get(source); if (t == null) { // 異??梢陨院笕ゲ东@ throw new IllegalArgumentException("No element matches " + source); } return t; } } }
然后再將這個(gè)工廠配置到項(xiàng)目中WebMvcConfigurationSupport:
@Override public void addFormatters(FormatterRegistry registry) { registry.addConverterFactory(new StringToEnumConverterFactory()); }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。