Spring Batch for Date和Double Fields的FlatFileParseException

时间:2017-08-19 18:51:31

标签: java spring spring-batch

我试图找出Spring Batch。我尝试从文件中读取数据但是异常。这是我的POJO:

public class VehicleData {

    private Long vehicleId;
    private Date timestamp;
    private Double latitude;
    private Double longitude;

    public VehicleData(Long vehicleId, Date timestamp, Double latitude, Double longitude) {
        this.vehicleId = vehicleId;
        this.timestamp = timestamp;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    //getters and setters

    }

文件的第一行:

1,2012-02-02 14:37:30,112.29369,43.92272

这是读者Bean:

@Bean
public FlatFileItemReader<VehicleData> reader() {
    FlatFileItemReader<VehicleData> reader = new FlatFileItemReader<>();
    reader.setResource(new ClassPathResource("data/1.txt"));
    reader.setLineMapper(new DefaultLineMapper<VehicleData>() {{
        setLineTokenizer(new DelimitedLineTokenizer() {{
            setNames(new String[]{"vehicleId", "timestamp", "latitude", "longitude"});
        }});

        setFieldSetMapper(new BeanWrapperFieldSetMapper<VehicleData>() {{
            setTargetType(VehicleData.class);
            setCustomEditors(Collections.singletonMap(Date.class,
                    new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), false)));
        }});
    }});
    return reader;
}

错误如下:

org.springframework.batch.item.file.FlatFileParseException: Parsing error at line: 1 in resource=[class path resource [data/1.txt]], input=[1,2012-02-02 14:37:30,112.29369,43.92272]
    at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:184) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:88) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
    at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:91) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]
    at org.springframework.batch.core.step.item.SimpleChunkProvider.read(SimpleChunkProvider.java:157) ~[spring-batch-core-3.0.8.RELEASE.jar:3.0.8.RELEASE]

...

Caused by: java.lang.reflect.UndeclaredThrowableException: null
    at org.springframework.util.ReflectionUtils.handleReflectionException(ReflectionUtils.java:285) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper.getBean(BeanWrapperFieldSetMapper.java:236) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
    at org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper.mapFieldSet(BeanWrapperFieldSetMapper.java:185) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
    at org.springframework.batch.item.file.mapping.DefaultLineMapper.mapLine(DefaultLineMapper.java:43) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
    at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:180) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
    ... 51 common frames omitted
Caused by: java.lang.InstantiationException: com.gom.tom.simulator.VehicleData
    at java.lang.Class.newInstance(Class.java:427) ~[na:1.8.0_102]
    at org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper.getBean(BeanWrapperFieldSetMapper.java:233) ~[spring-batch-infrastructure-3.0.8.RELEASE.jar:3.0.8.RELEASE]
    ... 54 common frames omitted
Caused by: java.lang.NoSuchMethodException: com.gom.tom.simulator.VehicleData.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_102]
    at java.lang.Class.newInstance(Class.java:412) ~[na:1.8.0_102]
    ... 55 common frames omitted

我对Date很怀疑并为此目的编写了一个自定义编辑器。可能是什么原因?

1 个答案:

答案 0 :(得分:2)

NoSuchMethodException: com.gom.tom.simulator.VehicleData.<init>()表示您缺少POJO中的默认构造函数。

public class VehicleData {
    // ....

    public VehicleData() {

    }

    // ....
}