简体   繁体   English

Spring 引导 MVC - 未调用枚举的自定义转换器编号

[英]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 ?有没有办法覆盖NumberEnum的默认转换器?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM