java - FlatFileParseException 解析错误 - Spring Batch

标签 java spring spring-boot spring-batch

我关注这个tutorial我收到 FlatFileParseException 错误:

org.springframework.batch.item.file.FlatFileParseException: Parsing error at line: 1 in resource=[class path resource [country.csv]], input=[AA,Aruba]

国家.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 类中缺少默认的无参数构造函数。

您正在使用 BeanWrapperFieldSetMapper映射 FieldSet到一个对象。引用 setTargetType(type) Java文档:

An object of this type will be created from its default constructor for every call to mapFieldSet(FieldSet).

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

关于java - FlatFileParseException 解析错误 - Spring Batch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33719747/

相关文章:

java - 无法解决此错误 : Failed to configure a DataSource

java - 我的 RESTful Web 服务不工作。(jersey + tomcat)

java - 如何在 DefaultOAuth2AuthorizationRequestResolver 上设置 AuthorizationRequestCustomizer?

java - BeanNotOfRequiredTypeException : Bean named X is expected to be of type X but was actually of type 'com. sun.proxy.$Proxy

java - spring @Transactional注解

spring-boot - 如何使用 Openshift secret 通过 SSL 建立 MQ 客户端连接

java - 如何正确关闭java客户端socket?

java - 你能用 JRockit 运行 glassfish 吗?

java - 在 java 中,如果仅移动最后一个数字项,则对整数数组进行排序的最佳方法是什么?

java.lang.NoClassDefFoundError : io/jsonwebtoken/Jwts - maven spring-boot