我正在使用 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
}
}