【问题标题】:FlatFileParseException at Spring Batch for Date and Double Fields日期和双字段的 Spring Batch 中的 FlatFileParseException
【发布时间】:2017-08-19 18:51:30
【问题描述】:

我正在尝试找出 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 持怀疑态度,并为此编写了一个自定义编辑器。这可能是什么原因?

【问题讨论】:

  • 你能把它写成答案吗?这是异常的原因。

标签: java spring spring-batch


【解决方案1】:

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

public class VehicleData {
    // ....

    public VehicleData() {

    }

    // ....
}

【讨论】:

    猜你喜欢
    • 2016-02-16
    • 2016-08-06
    • 1970-01-01
    • 2020-08-21
    • 2019-04-06
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 2022-06-30
    相关资源
    最近更新 更多