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

发布于 2025-01-01 00:14:13 字数 5800 浏览 1 评论 0原文

我在 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.StringToDate 是一个实现转换器

更新:

刚刚找到这个答案,这表明混合使用 ConverterPropertyEditor 可能会导致问题。我的应用程序中确实有部分使用PropertyEditor,但据我所知,文档并未讨论混合两个系统的任何问题。

堆栈跟踪:

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

}

但是,读取 以下内容论坛主题 我期望转换能够发挥作用。如果我理解正确,他们会说,一旦转换器设置正确,它就应该与 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

另请参阅我的原始问题< /a> (仍未解决)。

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 Converters and PropertyEditors might cause problems. I do have part in my app that use PropertyEditors, 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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木有鱼丸 2025-01-08 00:14:13

您确定应该使用 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

是 spring-core,另一个是 spring-webflow 转换器。您可能想尝试自己创建一个@Service

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

    public void ConversionService() {
        addDefaultConverters();
    }

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文