Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FlatFileParseException Parsing error - Spring Batch

I follow this tutorial and I'm getting FlatFileParseException error:

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

country.csv

AA,Aruba
BB,Baruba

and here is my ItemReader method

@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;
    }

and 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;
    }
}
like image 517
usil Avatar asked Nov 15 '15 12:11

usil


1 Answers

The problem here is that you are missing a default no-arg constructor in your Country class.

You are using BeanWrapperFieldSetMapper to map a FieldSet to an object. Quoting the setTargetType(type) Javadoc:

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

As such, you need to add a default constructor and provide the corresponding getter / setter for the properties.

like image 175
Tunaki Avatar answered Nov 09 '22 19:11

Tunaki