so I am creating my first Spring batch job. I am trying to read in an xml, manipulate some values, and write it out. My job seems to unmarshal my XML document and write it out fine if I do not implement the org.springframework.batch.item.ItemProcessor interface. Once I implement the interface, which I need to do in order to manipulate the values, I get the error below. When I check the output file it outputs the xml tag, the beginning root element tag, and nothing else. If I comment out the call to my processor in step1 of my job, then the file will write out to my file directory. Any help would be appreciated, I have been looking all over for a fix and haven't had any luck. My message validates against multiple large xsd's so I have left them out of the code.
2018-05-21 15:58:57.690 ERROR 6684 --- [ main] o.s.batch.core.step.AbstractStep : Encountered an error executing step step1 in job importUserJob java.lang.ClassCastException: javax.xml.bind.JAXBElement cannot be cast to [mySchema] BatchProcessor.process(BatchProcessor.java:1) ~[classes/:na]
public class BatchProcessor implements ItemProcessor<ProdRequest, ProdRequest>{
@Override
public ProdRequest process(ProdRequest arg0) throws Exception {
//Error still thrown even when I comment out all of the code. Break points in this methoda re never reached.
return arg0;
}
}
-
@Configuration
@EnableBatchProcessing
public class XMLReaderConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Bean
public BatchProcessor processor() {
return new BatchProcessor();
}
@Bean(destroyMethod="")
public StaxEventItemReader<ProdRequest> reader(){
StaxEventItemReader<ProdRequest> reader = new StaxEventItemReader<>();
reader.setResource(new ClassPathResource("policy.xml"));
reader.setFragmentRootElementName("ProdRequest");
Jaxb2Marshaller xmlMarshaller = new Jaxb2Marshaller();
ObjectFactory objectFactory = new ObjectFactory();
xmlMarshaller.setClassesToBeBound(new Class[] {
//all of my object classes (all generated by JAXB) I have added the xmlRootElement attribute to my top level class only.
});
xmlMarshaller.setSupportJaxbElementClass(true);
reader.setUnmarshaller(xmlMarshaller);
return reader;
}
@Bean(destroyMethod="")
public StaxEventItemWriter<ProdRequest> writer(){
StaxEventItemWriter<ProdRequest> writer = new StaxEventItemWriter<ProdRequest>();
String exportFilePath = "C:/Test/springBatchTest.xml";
writer.setResource(new FileSystemResource(exportFilePath));
writer.setRootTagName("ProdRequest");
Jaxb2Marshaller xmlMarshaller = new Jaxb2Marshaller();
xmlMarshaller.setClassesToBeBound(new Class[] {
//all of my object classes (all generated by JAXB) I have added the xmlRootElement attribute to my top level class only.
});
xmlMarshaller.setSupportJaxbElementClass(true);
writer.setMarshaller(xmlMarshaller);
return writer;
}
@Bean
public Step step1(){
return stepBuilderFactory.get("step1")
.<ProdRequest, ProdRequest> chunk(10)
.reader(reader())
.processor(processor())
.writer(writer())
.build();
}
@Bean
public Job importUserJob(){
return jobBuilderFactory.get("importUserJob")
.incrementer(new RunIdIncrementer())
.flow(step1())
.end()
.build();
}
}