Spring Batch configuration error in processor

kbo :

I want to configure Spring Batch job, but I receive the following error, how can I solve it?

Error: enter image description here

Reader:

import org.springframework.batch.item.ItemReader;

public class MoviesReader implements ItemReader<SearchResponseRO>, StepExecutionListener {

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

Processor:

import org.springframework.batch.item.ItemProcessor;

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

What do I need to change in order to fix the issue?

Thanks.

Brian Ecker :

You need to specify a type for the chunk operation. In your case that would be <SearchResponseRO, Movie>.

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

Without the type, it defaults to <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();

If you look at the definition of the chunk method, it accepts an int, but returns SimpleStepBuilder<I, O>. Because there is no way to actually provide the types for I and O, you have to essentially cast them to the values that you want. I believe that the .<Type> syntax is just convenience for the direct cast when chaining calls, so the following two things should be the same:

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=439071&siteId=1