Why not spring using my <String, Date> converter?
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 it
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 are required: a) Why is he not using my converter? b) if the date is zero, can the converter still call my converter?
I have it all while working with PropertyEditors, but I need to port to Converters.
But I can't understand why Spring MVC is not using my DateConverter. I implemented it so that if the source
+2
2 answers
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
public ResponseEntity<Resource<Object>> getStats(@RequestParam Date fromDate)
ConversionServiceFactoryBean
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html
, JODA, . ( ) ConversionServiceFactoryBean
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="MyStringToDateConverter"/>
</list>
</property>
</bean>
mvc-annotation:
<mvc:annotation-driven conversion-service="conversionService"/>
, . " ".
, Spring MVC 3.x, init.
/**
* 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));
}
.
+3