Reputation: 12230
I have the following code in one of the Spring's XML config files:
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="org.springframework.binding.convert.converters.StringToDate">
<property name="pattern" value="yyyy-MM-dd" />
</bean>
</list>
</property>
</bean>
But I am getting the following exception during deployment (on JBoss):
java.lang.IllegalArgumentException: Each converter object must implement one of the Converter, ConverterFactory, or GenericConverter interfaces
Any idea why? As far as I can see, org.springframework.binding.convert.converters.StringToDate is an implementation of Converter
.
UPDATE:
Just found this answer, that suggests that mixing Converter
s and PropertyEditor
s might cause problems. I do have part in my app that use PropertyEditor
s, but as far as I can see, the documentation does not talk about any problem with mixing the two systems.
Stack trace:
Caused by: java.lang.IllegalArgumentException: Each converter object must implement one of the Converter, ConverterFactory, or GenericConverter interfaces
at org.springframework.core.convert.support.ConversionServiceFactory.registerConverters(ConversionServiceFactory.java:106)
at org.springframework.context.support.ConversionServiceFactoryBean.afterPropertiesSet(ConversionServiceFactoryBean.java:56)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 146 more
UPDATE 2:
I changed my xml to be:
<mvc:annotation-driven conversion-service="conversionService" />
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="my.project.StringToDate">
<!-- org.springframework.binding.convert.converters.StringToDate DEFAULT_PATTERN = "yyyy-MM-dd" -->
<property name="pattern" value="yyyy-MM-dd" />
</bean>
</set>
</property>
</bean>
My custom converter is:
package my.project;
import java.util.Date;
import org.springframework.core.convert.converter.Converter;
public class StringToDate extends org.springframework.binding.convert.converters.StringToDate implements Converter<String, Date> {
public Date convert(String source) {
Date date = null;
try {
date = (Date) convertSourceToTargetClass(getPattern(), getTargetClass());
} catch (Exception e) {
}
return date;
}
}
HOWEVER, reading the following forum thread I would expect the conversion to work. If I get them right, they are saying that once the converter is set correctly, it should be working with Spring Batch, i.e. it does not require any special setting to make it work specifically with Spring Batch. But I am still getting a BindException during the batch task... any idea why?
See the new stack trace:
Caused by: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'target' on field 'datetimeInactive': rejected value [2011-04-27]; codes [typeMismatch.target.datetimeInactive,typeMismatch.datetimeInactive,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [target.datetimeInactive,datetimeInactive]; arguments []; default message [datetimeInactive]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'datetimeInactive'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'datetimeInactive': no matching editors or conversion strategy found]
Field error in object 'target' on field 'datetimeActive': rejected value [2011-04-27]; codes [typeMismatch.target.datetimeActive,typeMismatch.datetimeActive,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [target.datetimeActive,datetimeActive]; arguments []; default message [datetimeActive]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'datetimeActive'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'datetimeActive': no matching editors or conversion strategy found]
at org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper.mapFieldSet(BeanWrapperFieldSetMapper.java:186)
at org.springframework.batch.item.file.mapping.DefaultLineMapper.mapLine(DefaultLineMapper.java:42)
at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:179)
... 45 more
See also my original question (still unsolved).
Upvotes: 5
Views: 4288
Reputation: 40256
Are you sure you should be using the org.springframework.context.support.ConversionServiceFactoryBean
? This is a spring-core defined class. It expects the following:
org.springframework.core.convert.converter.GenericConverter
org.springframework.core.convert.converter.Converter
org.springframework.core.convert.converter.ConverterFactory
You are trying to send it a
org.springframework.binding.convert.converters.Converter
One is a spring-core, the other is a spring-webflow converter. You may want to try and create a @Service yourself
@Service("conversionService")
public class MyConversionService extends DefaultConversionService {
public void ConversionService() {
addDefaultConverters();
}
@Override
protected void addDefaultConverters() {
super.addDefaultConverters();
}
}
Upvotes: 3