Amir Raminfar
Amir Raminfar

Reputation: 34169

Why isn't spring using my Converter<String, Date>?

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

Upvotes: 2

Views: 9984

Answers (2)

Anders Eriksson
Anders Eriksson

Reputation: 593

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.

Upvotes: 3

Ralph
Ralph

Reputation: 120811

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.

Upvotes: 3

Related Questions