没有合适的资源?快使用搜索试试~ 我知道了~
首页spring boot 枚举使用的坑整理
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/12747188/bg1.jpg)
spring boot 枚举使用的坑整理枚举使用的坑整理
在本篇文章里我们给大家整理了关于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<T extends Enum> implements Converter<String, T> {
private final Class<T> enumType;
public StringToEnum(Class<T> 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<String, BaseEnum> {
private static final Map<Class, Converter> converterMap = new HashMap<>();
@Override
public <T extends BaseEnum> Converter<String, T> getConverter(Class<T> targetType) {
Converter<String, T> converter = converterMap.get(targetType);
if(converter == null) {
converter = new StringToEnumConverter<>(targetType);
converterMap.put(targetType, converter);
}
return converter;
}
class StringToEnumConverter<T extends BaseEnum> implements Converter<String, T> {
private Map<String, T> enumMap = new HashMap<>();
StringToEnumConverter(Class<T> 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());
}
意思就是string 转 BaesEnum都走这个converter。
至此这个坑就算解决了。
以上就是本次介绍的全部知识点内容,感谢大家对我们的支持。
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/default.jpg!1)
weixin_38557896
- 粉丝: 0
- 资源: 971
上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
会员权益专享
最新资源
- 新皇冠假日酒店互动系统的的软件测试论文.docx
- 上海空中营业厅系统的软件测试论文.doc
- 网上选课系统的设计与实现论文.doc
- 师生互动网站设计与实现 ()论文.doc
- 学生档案管理系统论文_正文.doc
- 视频会议系统的设计与实现毕业论文.docx
- 基于web的职工电子档案管理系统的设计与实现毕业论文.docx
- 考试辅导网站的设计与实现论文.doc
- 论文 云端图书馆管理系统设计与实现.docx
- 计算机等级考试网上辅导系统的设计与实现论文.doc
- 基于web烘焙坊网站的设计与实现论文.doc
- 论文_基于J2EE的高校后勤采供管理系统开发.docx
- 老龄化社区服务及其系统应用论文.doc
- 论文-java基于SSM的大学生创新创业信息系统.docx
- 猎豹安全浏览器的软件测试论文.doc
- 基于Web的网上书店系统的设计与实现毕业论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)