[英]Spring Boot MVC - Custom Converter Number to Enum not called
I tried converting a Number
to an Enum
Type with a custom implementation of a Converter<Number, MyEnum>
in a Spring Boot MVC application.我尝试在 Spring Boot MVC 应用程序中使用
Converter<Number, MyEnum>
的自定义实现将Number
转换为Enum
类型。 Unfortunately the converter isn't called.不幸的是,转换器没有被调用。 Instead spring converts the number as the ordinal number of the enum.
相反,spring 将数字转换为枚举的序数。
The converter is registered by a @Component
annotation.转换器由
@Component
注释注册。
Is there a way to override the default converter for Number
and Enum
?有没有办法覆盖
Number
和Enum
的默认转换器?
you can reference to the built-in StringToEnumConverterFactory
and write a custom converter factory, then register it.您可以参考内置的
StringToEnumConverterFactory
并编写自定义转换器工厂,然后注册它。
The key is String
of StringToEnumConverterFactory implements ConverterFactory<String, YourEnum>
, instead of ConverterFactory<Integer, YourEnum>
.关键是
StringToEnumConverterFactory implements ConverterFactory<String, YourEnum>
的String
实现了 ConverterFactory<String, YourEnum> ,而不是ConverterFactory<Integer, YourEnum>
。
I used Integer
before, but my custom converter is ignored.之前用过
Integer
,但是我的自定义转换器被忽略了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.