IllegalStateException:无法将类型的值转换为所需类型



这是我收到的错误。

       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>

这是接口(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转换为FieldSetMaper(这只是一个接口,我知道这不起作用(。

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

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

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

应该是

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

查看您的bean声明。

        <property name="fieldSetMapper">
            <bean class="code.ProductFieldSetMapper" />
        </property>

以下是经过一些更正的代码:

Runner.java(使用DelimitedLineTokenizer类将逗号分隔的字符串标记为FieldSet,然后通过ProductFieldSetMapper类将其与对象(Product(映射(

ApplicationContext context = new ClassPathXmlApplicationContext(
        "FileReaderConfig.xml");
ProductFieldSetMapper obj = (ProductFieldSetMapper) context.getBean("fieldSetMapper");
DelimitedLineTokenizer tokenizer = (DelimitedLineTokenizer) context
        .getBean("lineTokenizer");
FieldSet fieldSet = tokenizer.tokenize("1,Pepsi,Cold drinks,30");
Product product = (Product) obj.mapFieldSet(fieldSet);
System.out.println(product.getDescription() + "-" + product.getId() + "-"
        + product.getName());

配置xml文件:(无需声明除下面定义的两个之外的任何bean或作业,因为您在Main类中的任何位置都没有使用它(

<bean id="lineTokenizer"
    class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
    <property name="names" value="PRODUCT_ID,NAME,DESCRIPTION,PRICE" />
</bean>
<bean id="fieldSetMapper" class="com.spring.batch.domain.ProductFieldSetMapper" />

ProductFieldSetMapper.java:(无需定义自定义FieldSetMapper(

import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
public class ProductFieldSetMapper implements org.springframework.batch.item.file.mapping.FieldSetMapper<Product> {
    public Product mapFieldSet(FieldSet fieldSet) throws BindException {
        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;
    }
}

有关详细示例,请在此处阅读,并使用春季批处理作业提供额外功能

相关内容

  • 没有找到相关文章

最新更新