Spring 批处理 ItemReader 在第一行获取 NullPointerException

Spring Batch ItemReader gets NullPointerException in first line

当我尝试读取我的 csv 文件时,Spring 批处理遇到了这个问题。

我必须阅读具有如下结构的 CSV

valueHeader;valueHeader;valueHeader;valueHeader;valueHeader;valueHeader
conventValue;conventValue;conventValue;conventValue;conventValue
conventValue;conventValue;conventValue;conventValue;conventValue
conventValue;conventValue;conventValue;conventValue;conventValue
valueFooter;valueFooter;valueFooter;valueFooter;valueFooter;valueFooter

我有一个 MultiResourceItemReader 委托给一个 FlatFileItemReader 类型的字符串,以便轻松地将此行映射到我的自定义 DTO之后执行的自定义项目处理器。

在我的 Tasklet 中,我像这样注入了我的 MultiResourceItemReader bean:

@Autowired
    private MultiResourceItemReader<String> resourceItemReader;

然后我这样调用它(设置资源和上下文):

    resourceItemReader.setStrict(Boolean.FALSE);
    resourceItemReader.setResources(resources);
    resourceItemReader.open(context.getStepContext().getStepExecution().getExecutionContext());

    String currentContent = resourceItemReader.read();
    while(currentContent != null) {
        linesReaded.add(currentContent);
        currentContent = resourceItemReader.read();
    }

But I am getting a **NullPointerException** in the first Line and I don't know why:

20190705 19:53:30.041 INFO   com.mycomp.mycustomp.tasklet.MyCustomTasklet [[ # ]] - [MyCustomTasklet:EXECUTE]
20190705 19:53:41.560 ERROR  org.springframework.batch.core.step.AbstractStep [[ # ]] - Encountered an error executing step processRebateASLIStep in job MyProyectJob
org.springframework.batch.item.file.FlatFileParseException: Parsing error at line: 1 in resource=[file [C:\Pruebas\MyProyectJob\destino\MyFile-100-20151130143000-000001.csv]], input=[HEADER;PEP;FacteTRY;100; 20151130143000;000125;100000000000000000000000000000000025874]
    at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:183)
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:88)
    at org.springframework.batch.item.file.MultiResourceItemReader.readFromDelegate(MultiResourceItemReader.java:140)
    at org.springframework.batch.item.file.MultiResourceItemReader.readNextItem(MultiResourceItemReader.java:119)
    at org.springframework.batch.item.file.MultiResourceItemReader.read(MultiResourceItemReader.java:108)
....
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.lang.NullPointerException: null
    at org.springframework.batch.item.file.mapping.DefaultLineMapper.mapLine(DefaultLineMapper.java:43)
    at org.springframework.batch.item.file.FlatFileItemReader.doRead(FlatFileItemReader.java:180)
    ... 59 common frames omitted

我也会附上我的 readerItems bean:

@Bean
        MultiResourceItemReader<String> resourceItemReader(FlatFileItemReader<String> flatFileItemReader){
            MultiResourceItemReader<String> resourceItemReader = new MultiResourceItemReader<>();
            resourceItemReader.setDelegate(flatFileItemReader);
            resourceItemReader.setStrict(false);
            return resourceItemReader;
        }

    @Bean
    FlatFileItemReader<String> flatFileItemReader() {

        FlatFileItemReader<String> reader = new FlatFileItemReader<String>();
        reader.setStrict(false);
        reader.setLineMapper(new DefaultLineMapper<String>() {
        {
            //Setea los valores en la clase indicada
            setFieldSetMapper(new FieldSetMapper<String>() {

                @Override
                public String mapFieldSet(FieldSet fieldSet) throws BindException {
                    String[] values = fieldSet.getValues();
                    return String.join(";", values);
                }

            });

            }
        });

        return reader;
    }

您没有在 LineMapper 上设置行分词器。这是添加一个的方法:

    reader.setLineMapper(new DefaultLineMapper<String>() {
        {
            //Setea los valores en la clase indicada
            setFieldSetMapper(new FieldSetMapper<String>() {

                @Override
                public String mapFieldSet(FieldSet fieldSet) throws BindException {
                    String[] values = fieldSet.getValues();
                    return String.join(";", values);
                }

            });

            setLineTokenizer(new DelimitedLineTokenizer(";"));

        }
    });

我建议使用 FlatFileItemReaderBuilder,由于其流畅 API,它更直观。这是一个例子:

@Bean
public FlatFileItemReader<Pojo> flatFileItemReader() {
    return new FlatFileItemReaderBuilder<Pojo>()
            .name("itemReader")
            .resource(new FileSystemResource("/path/to/flat/file"))
            .targetType(Pojo.class)
            .delimited()
            .delimiter(";")
            .names("column1", "column2")
            .build();
}