FlatFileParseException

FlatFileParseException

本文介绍了FlatFileParseException解析错误-Spring批处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循此tutorial,但收到FlatFileParseException错误:

country.csv

AA,Aruba
BB,Baruba

这里是我的ItemReader方法

@Bean
    public ItemReader<Country> reader() {
        FlatFileItemReader<Country> reader = new FlatFileItemReader<Country>();
        reader.setResource(new ClassPathResource("country.csv"));
        reader.setLineMapper(new DefaultLineMapper<Country>() {{
            setLineTokenizer(new DelimitedLineTokenizer() {{
                setNames(new String[] { "countryCode", "countryName" });
            }});
            setFieldSetMapper(new BeanWrapperFieldSetMapper<Country>() {{
                setTargetType(Country.class);
            }});
        }});
        return reader;
    }

Country.java

@Entity
@Table(name="Country")
public class Country  {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, updatable = false)
    Long id;

    @Column(name = "countryCode", nullable = false, updatable = false)
    String countryCode;

    @Column(name = "countryName", nullable = false, updatable = false)
    String countryName;

    public Country(String countryCode, String countryName) {
        this.countryCode = countryCode;
        this.countryName = countryName;

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCountryCode() {
        return countryCode;
    }

    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    @Override
    public String toString() {
        return "countryCode: " + countryCode + ", countryName: " + countryName;
    }
}

推荐答案

此处的问题是您的Country类中缺少默认的无参数构造函数。

您正在使用BeanWrapperFieldSetMapperFieldSet映射到对象。引用setTargetType(type)Javadoc:

因此,您需要添加默认构造函数并为属性提供相应的getter/setter。

这篇关于FlatFileParseException解析错误-Spring批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-21 17:22