处理器中的Spring Batch配置错误

时间:2017-05-15 18:47:14

标签: java spring spring-batch

我想配置Spring Batch作业,但是我收到以下错误,我该如何解决?

错误: enter image description here

阅读器:

import org.springframework.batch.item.ItemReader;

public class MoviesReader implements ItemReader<SearchResponseRO>, StepExecutionListener {

    @Override
    public SearchResponseRO read() throws Exception {
        return new SearchResponseRO();
    }
}

处理器:

import org.springframework.batch.item.ItemProcessor;

public class MoviesProcessor implements ItemProcessor<SearchResponseRO, Movie> {
    @Override
    public Movie process(SearchResponseRO searchResponseRO) throws Exception {
        return new Movie();
    }
}

为了解决问题,我需要更改哪些内容?

感谢。

1 个答案:

答案 0 :(得分:13)

您需要为chunk操作指定类型。在您的情况下,<SearchResponseRO, Movie>

return stepBuilderFactory.get("downloadStep").<SearchResponseRO, Movie>chunk(10)
  .reader(reader)
  .processor(processor)
  .....

如果没有该类型,则默认为<Object, Object>

stepBuilderFactory.get("test").chunk(10)
        .reader(new ItemReader<Object>() {
            @Override
            public Object read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
                return null;
            }
        })
        .processor(new ItemProcessor<Object, Object>() {
            @Override
            public Object process(Object o) throws Exception {
                return null;
            }
        })
        .writer(new ItemWriter<Object>() {
            @Override
            public void write(List<?> list) throws Exception {

            }
        })
        .build();

如果查看chunk方法的定义,它会接受int,但会返回SimpleStepBuilder<I, O>。由于无法实际提供IO的类型,因此您必须将它们基本上转换为您想要的值。我相信.<Type>语法只是在链接调用时直接转换的方便,所以以下两件事应该是相同的:

public void castGenericReturnType() {
    System.out.println(this.<Integer>genericReturn(1));
    System.out.println((Integer) genericReturn(1));
}

public <I> I genericReturn(Object objectToCast) {
    return (I) objectToCast;
}