2

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

Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
  • Can you post the source for the class from which this exception is thrown? – abalogh May 12 '11 at 21:32
  • It looks correct. But I found one strange thing: The examples I found and the Reference (5.5.5 Configuring a ConversionService) uses a {{}} instead of {{>}}. Even if the method parameter is Set. – Ralph May 13 '11 at 09:13
  • Do you mix Converters and PropertyEditors? – Ralph May 13 '11 at 09:14
  • Hey everybody, thanks for helping. I have searched for "PropertyEditor" in my code base and all the use is in one package. This package was enabled in my xml file using AnnotationMethodHandlerAdapter but it is disabled. I have confirmed no where else is using it. – Amir Raminfar May 13 '11 at 13:37
  • UPDATE! I think my converter is being used now. So I am little confused. But what doesn't work is that I want to be pass default value. For example, if no date is provided then set it to now. This used to work with PropertyEditors. Is it not possible with converters? – Amir Raminfar May 13 '11 at 14:12

2 Answers2

3

I also got

2012-06-26 12:41:55,215 DEBUG DefaultHandlerExceptionResolver - Resolving exception from handler [public org.springframework.http.ResponseEntity<Resource<java.lang.Object>> StatisticsController.getStats(java.util.Date)]: 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 

when trying to call to my controller method

public ResponseEntity<Resource<Object>> getStats(@RequestParam Date fromDate)

Reading about ConversionServiceFactoryBean on

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html

indicates that date support requires JODA time which we didn't want. So I tried to add my own converter (that implements Converter) to ConversionServiceFactoryBean

<bean id="conversionService"
      class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <list>
            <bean class="MyStringToDateConverter"/>
        </list>
    </property>
</bean>

and also bound this to the mvc-annotation driven tag:

<mvc:annotation-driven  conversion-service="conversionService"/> 

But I never got my converter to kick in. I always got "no matching editors or conversion strategy found".

As indicated above I was hoping to use the Spring MVC 3.x style of conversion service but I had to go for the workaround with the old approach with init binder.

/**
 * I would claim this is should not be needed in Spring 3.x with ConversionServiceFactoryBean
 * @param binder
 */
@InitBinder
public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}

This worked immediately.

Anders Eriksson
  • 533
  • 5
  • 14
3

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

If no ConversionService is registered with Spring, the original PropertyEditor-based system is used.

Ralph
  • 118,862
  • 56
  • 287
  • 383