我正在使用 Spring 网络流。
我有一个FormattingConversionService配置。在此转换服务中,我配置了以下内容:
- A
ConverterFactory将字符串值转换为MyInterface实例(绑定到对象) - A
Converter将对象转换MyInterface为字符串(用于显示)
'ConverterFactory' 被调用并且运行良好。
我的问题是Converter没有调用。显示toString()在页面上。
如何让 Spring 将对象实例转换MyInterface为String用于显示目的?
这是我的conversionService声明:
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="myclasses.StringToMyInterfaceConverterFactory"/>
<bean class="myclasses.MyInterfaceToStringConverter"/>
</set>
</property>
</bean>
MyInterfaceToStringConverter:
@Component
public class MyInterfaceToStringConverter<T extends MyInterface> implements Converter<T, String> {
public String convert(T source) {
if (source == null) {
return null;
}
return source.getCode(); // This is a method in MyInterface which returns a String
}
}