Spring Webflow绑定:Converter - java.lang.IllegalArgumentException:每个转换器对象必须实现一个Converter ...接口

时间:2012-01-30 21:25:57

标签: java spring exception spring-webflow spring-webflow-2

我在Spring的一个XML配置文件中有以下代码:

  <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>

但是我在部署期间遇到以下异常 (在JBoss上):

  

java.lang.IllegalArgumentException:每个转换器对象必须   实现Converter,ConverterFactory或GenericConverter之一   接口

知道为什么?据我所知,org.springframework.binding.convert.converters.StringToDateConverter的实现。

更新

刚刚找到this回答,这表明混合ConverterPropertyEditor可能会导致问题。我确实参与了使用PropertyEditor s的应用程序,但据我所知,文档没有讨论混合这两个系统的任何问题。

堆栈跟踪:

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

更新2:

我将我的xml改为:

  <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>

我的自定义转换器是:

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;
    }

}

然而,阅读the following forum thread我希望转换能够奏效。如果我说得对,他们说一旦转换器设置正确,它应该使用Spring Batch,即它不需要任何特殊设置就可以使它专门用于Spring Batch。但是我在批处理任务期间仍然遇到BindException ...任何想法为什么?

查看新的堆栈跟踪:

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

另请参阅我的original question(尚未解决)。

1 个答案:

答案 0 :(得分:3)

您确定应该使用org.springframework.context.support.ConversionServiceFactoryBean吗?这是一个Spring-core定义的类。它预计如下:

org.springframework.core.convert.converter.GenericConverter
org.springframework.core.convert.converter.Converter
org.springframework.core.convert.converter.ConverterFactory

您正尝试发送

org.springframework.binding.convert.converters.Converter

一个是弹簧芯,另一个是弹簧 - 流动转换器。您可能想尝试自己创建一个@Service

@Service("conversionService")
public class MyConversionService extends DefaultConversionService {

    public void ConversionService() {
        addDefaultConverters();
    }

    @Override
    protected void addDefaultConverters() {
        super.addDefaultConverters();
    }

}