java - Spring Webflow 绑定(bind) : Converter - java. lang.IllegalArgumentException : Each converter object must implement one of the Converter . .. 接口(interface)

标签 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: Each converter object must implement one of the Converter, ConverterFactory, or GenericConverter interfaces

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

更新:

刚找到this答案,这表明混合使用 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;
    }

}

但是,阅读 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 (仍未解决)。

最佳答案

您确定应该使用 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();
    }

}

关于java - Spring Webflow 绑定(bind) : Converter - java. lang.IllegalArgumentException : Each converter object must implement one of the Converter . .. 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9070982/

相关文章:

java - Spring Boot 忽略来自外部 application.properties 的日志记录级别

c++ - 在 C++ 中调度异常

java - 正则表达式替换 Spring @RequestMapping URL 中的 @PathVariable 值

java - 单击复选框后如何调用 Activity ?

java - Android - 创建动态 TextView

java - 生成的pdf文件保存在项目文件夹内

java - 每次集成测试后 Spring 上下文变脏

java - 如何检查一周内特定列的所有值

java - 与异常处理相关的查询

c# - HRESULT 异常 : 0x80004002 (E_NOINTERFACE)