spring boot 枚举使用的坑整理

网友投稿 604 2022-12-29


spring boot 枚举使用的坑整理

java 枚举的功能挺多,但是坑更多,使用的时候要注意。如下面这个枚举。

@Getter

@AllArgsConstructor

public enum EnumExpenseType implements BaseEnum {

小欢喜(1),

大欢喜(2);

private final int value;

}

咋一看,没什么问题,但是具体使用过程中,总是会出问题。原因就是这个枚举没有按照从0开始索引,除此之外即使从0开始,中间有断的索引也会有问题。主要出现在以下方面:

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 StringToEnum implements 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);

}

}

http://

@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());

}

意思就是string 转 BaesEnum都走这个converter。

至此这个坑就算解决了。

以上就是本次介绍的全部知识点内容,感谢大家对我们的支持。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:视频解析接口测试工具(视频解析接口测试工具在哪)
下一篇:系统接口设计逻辑关系图(系统接口设计案例)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~