我有很多枚举实现了一个名为 Codeable 的接口。我想在从 json 反序列化并尝试使用 ConverterFactory 时进行反向查找
public class StringToCodeableConverterFactory implements ConverterFactory<String, Codeable> {
@Override
public <T extends Codeable> Converter<String, T> getConverter(
Class<T> targetType) {
return new StringToCodeableConverter<T>(targetType);
}
private final class StringToCodeableConverter<T extends Codeable> implements Converter<String, T> {
private Class<T> enumType;
public StringToCodeableConverter(Class<T> enumType) {
this.enumType = enumType;
}
@Override
public T convert(String source) {
return CodeableUtil.get(this.enumType, source);
}
}
}
这是弹簧配置
<!-- Custom Converters from String to Java Type-->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.duetto.model.StringToCodeableConverterFactory" />
</list>
</property>
</bean>
<mvc:annotation-driven conversion-service="conversionService">
经过一番挖掘,我发现 Spring 采用默认的 StringToEnumConverterFactory 而不是我的 StringToCodeableConverterFactory,为什么会这样?我怎样才能让我的优先于另一个?