Reputation: 1130
When I run a job with the new Spring Batch 5 RC1, it comes always to the following error:
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [my.package.FileProvider] to type [java.lang.String]
So, it seems that I need to provide a GenericConverter
. But the standard ways aren't working.
If I register them via:
@Configuration
public class ConverterRegistration implements WebMvcConfigurer {
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new FileConverter<>());
registry.addConverterFactory(new FileConverterFactory());
}
}
it doesn't work. And even if I extend DefaultBatchConfiguration
and set the converter in a @PostConstruct
method with calls like getConversionService().addConverter(new FileConverter())
,
my debugger shows that the list of converters stays always the same, at the point where the exception comes from: GenericConversionService
. It seems that the Jobs have their own local list of converters. A first stop at a breakpoint shows that GenericConversionService
has 129 converters, including my custom ones, and at a later stop, when the exceptions gets thrown, it has always 52 converters.
How do I add a converter there?
At the JobBuilder?
return new JobBuilder(JOB_IMPORT, jobRepository)
.incrementer(new RunIdIncrementer())
.start(infoImport)
.end()
.build();
At the step builder?
new StepBuilder(getStepName(), jobRepository)
.<I, O>chunk(chunkSize, platformTransactionManager)
.listener(readListener)
.reader(reader)
.processor(processor)
.writer(writer)
Most likely the job parameters, but how?
JobParameters jobParameters = new JobParametersBuilder()
.addJobParameter(FILE_PROVIDER,
new JobParameter<>(fileProvider, FileProvider.class))
.addString(INFO_FILE_NAME, fileInfo)
.toJobParameters();
jobLauncher.run(fileImportJob, jobParameters);
Can somebody show me where and how I can set my custom GenericConverter
?
Or is it somehow a JobParametersConverter
which is needed, but then: How to set that
(the documentation at Spring Batch 5 RC1, Java Config seems incomplete)?
Upvotes: 2
Views: 1412
Reputation: 1130
I found it, pretty obvious once you see it. Override DefaultBatchConfiguration#getConversionService()
, like this:
@Override
protected ConfigurableConversionService getConversionService() {
ConfigurableConversionService conversionService = super.getConversionService();
conversionService.addConverter(new MyOwnConverter<>());
return conversionService;
}
Upvotes: 0
Reputation: 31710
There is no relation between web converters and batch converters. You should not be expecting that converters registered through WebMvcConfigurer
are going to be used by Spring Batch.
And even if I extend DefaultBatchConfiguration and set the converter in a @PostConstruct method with calls like getConversionService().addConverter(new FileConverter()),
This won't work as getConversionService()
always return a new instance of DefaultConversionService
. You need to override DefaultBatchConfiguration#getConversionService
and return the instance of the conversion service you want to use for your job parameters, including all your converters. Here is a typical example:
@Configuration
public class MyJobConfiguration extends DefaultBatchConfiguration {
@Bean
public Job job(JobRepository jobRepository) {
// define your job
return null;
}
@Override
protected ConfigurableConversionService getConversionService() {
DefaultConversionService conversionService = new DefaultConversionService();
// add converters to conversionService
return conversionService;
}
}
Upvotes: 4