java - 非法状态异常 : Cannot convert value of type to required type

标签 java spring spring-batch

这是我收到的错误。

       Caused by: java.lang.IllegalStateException: Cannot convert value of type [code.ProductFieldSetMapper] to required type [org.springframework.batch.item.file.mapping.FieldSetMapper] for property 'FieldSetMapper': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:264)
at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:450)
... 23 more

这是我的上下文文件 (FileReaderConfig.xml)

        <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:batch="http://www.springframework.org/schema/batch"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/batch
    http://www.springframework.org/schema/batch/spring-batch.xsd">

<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
    <property name="resource" value="file:./output.txt" />
    <property name="linesToSkip" value="1" />
    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <property name="lineTokenizer">
                <bean
                    class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="names" value="PRODUCT_ID,NAME,DESCRIPTION,PRICE" />
                </bean>
            </property>
            <property name="fieldSetMapper">
                <bean class="code.ProductFieldSetMapper" />
            </property>
        </bean>
    </property>
</bean>
<job id="importProducts" xmlns="http://www.springframework.org/schema/batch">
    <step id="readWriteProducts">
        <tasklet>
            <chunk reader="reader" writer="writer" commit-interval="100" />
        </tasklet>
    </step>
</job>

这是接口(interface)(FieldSetMapper.java) 封装代码;

 import org.springframework.batch.item.file.transform.FieldSet;
 import org.springframework.validation.BindException;

 public interface FieldSetMapper<T> {
T mapFieldSet(FieldSet fieldSet) throws BindException;
 }

这是 ProductFieldSetMapper.java

 package code;

import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;

public class ProductFieldSetMapper implements FieldSetMapper<Product> {

public Product mapFieldSet(FieldSet fieldSet) throws BindException {
    // TODO Auto-generated method stub
    Product product = new Product();
    product.setId(fieldSet.readString("PRODUCT_ID"));
    product.setName(fieldSet.readString("NAME"));
    product.setDescription(fieldSet.readString("DESCRIPTION"));
    product.setPrice(fieldSet.readBigDecimal("PRICE"));
    return product;
}

}

这是我正在运行的类(Runner.java) 封装代码;

import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.validation.BindException;

public class Runner {

public static void main(String[] args) throws BeansException, BindException {
    // TODO Auto-generated method stub
    Product product;
     ApplicationContext context = 
             new ClassPathXmlApplicationContext("FileReaderConfig.xml");

    ProductFieldSetMapper obj = (ProductFieldSetMapper) context.getBean("FieldSetMapper");

     product = (Product) obj.mapFieldSet((FieldSet)context.getBean("lineTokenizer"));

System.out.println(product.getDescription() + ""+product.getId()+""+product.getName());


}
}

我不明白我的代码在哪里(或者为什么)尝试将 ProductFieldSetMapper 转换为 FieldSetMapper(这只是一个接口(interface),我知道这不起作用)。

顺便说一句,Product.java 是一个带有变量及其各自的 setter 和 getter 的 POJO。

最佳答案

该错误是由于我使用自己的接口(interface)而不是 Spring 提供的接口(interface)造成的。我删除了接口(interface)类,并在导入后让 ProductFieldSetMapper 实现 org.springframework.batch.item.file.mapping.FieldSetMapper 。这解决了问题。

关于java - 非法状态异常 : Cannot convert value of type to required type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22389235/

相关文章:

java - 类型不匹配 : cannot convert from Set<Object> to Set<Long>

spring-batch - Spring XD : pipe (>) from file source to batch job fails (IllegalArgumentException: Unable to convert provided JSON to Map<String, 对象>)

Spring Batch 为 H2 在 SQL 语句中创建语法错误

java - 如何在 tomcat 服务器启动时运行长时间运行的批处理作业?

java.lang.NoClassDefFoundError : org/slf4j/LoggerFactory even though I have the right dependencies

java - Windows 8.1。 - Java - ODBC 驱动程序 - NetBeans

java - 是否可以从 SWT 到 JavaFx 使用 NatTable 组件?

spring - 如何在非响应式(Reactive) Spring EventListener 和响应式(Reactive) Flux 之间架起桥梁

java - ORA-01000 : maximum open cursors exceededwhen using Spring SimpleJDBCCall

java - spring-data-jpa 和 spring-boot-starter-data-jpa 的区别