I have this in my applicationContext.xml
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="mycompany.AsOfDateConverter"/>
<bean class="mycompany.CustomerConverter"/>
<bean class="mycompany.FooConverter"/>
</set>
</property>
</bean>
AsOfDateConverter looks like
public class AsOfDateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
if(source == null) return new Date();
//... else parse date. not shown.
}
}
But Spring never picks up my DateConverter. Instead I get this
org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date]: no matching editors or conversion strategy found
at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:53)
at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:534)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestParam(HandlerMethodInvoker.java:506)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:339)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:170)
Two solutions needed:
a) Why isn’t it using my converter?
b) if date is null then can converter still call my converter?
I have all this working with PropertyEditors but wanted to port to Converters.
But I can’t figure out why Spring MVC does not use my DateConverter. I have it implemented so that if source
I can give you a hint:
The Exception is thrown from the class ´org.springframework.beans.SimpleTypeConverter´.
This class belongs to the PropertyEditor Support (Framwork) but not to the ConversionService Framework.
So it seam that mixing both does not work like you want, or the ConversionService is not enabled:
5.5.5 Configuring a ConversionService