如何在springboot项目中使用枚举
如何在spring boot项目中使用枚举?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
成都创新互联主要从事做网站、成都网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务泸县,10年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792
1. 在controller的方法中,比如以这个枚举为参数,如下代码:
@RequestMapping("/**") public String getRejectReasons(EnumExpenseType type) { return ""; }
前台传入的参数如果是type:1, 那它值应该是:小欢喜,实际上呢?
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
实际上它却报了个错。转换失败了。
查看报错信息,可以定位到是spring框架中StringToEnumConverterFactory中转换失败,具体代码如下:
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这里报错,Enum.valueOf的后面的值并不是我们的value,而是name(这里的小欢喜)。
所以,我们不能使用这个spring提供converter,需要自定义一个: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; } } }
然后再将这个工厂配置到项目中WebMvcConfigurationSupport:
@Override public void addFormatters(FormatterRegistry registry) { registry.addConverterFactory(new StringToEnumConverterFactory()); }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。
新闻标题:如何在springboot项目中使用枚举
网站路径:http://scyanting.com/article/gjojgd.html