2

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();               
}
}
Kachopsticks
  • 125
  • 1
  • 13

1 Answers1

1

so eventually I set a break point within in the spring framework and found that after my document was getting unmarshalled it was in fact returning a generic JAXBElement which contained the document that I was expecting. When that JAXBElement was getting passed to my BatchProcessor class which was implementing the Spring ItemProcessor, the error was being thrown because my class was not expecting the document to be inside a JAXBElement. So In order to fix this I had to change the parameter for my BatchProcessor to be JAXBElement. MY fix is below. I have removed some company specific names and my list of JAXBClasses. Hopefully this helps someone down the road. Thanks!

public class BatchProcessor implements ItemProcessor<JAXBElement<ProdRequest> , ProdRequest>{

@Override
public ProdRequest process(JAXBElement<ProdRequest>  jaxbElement) throws Exception {
    ObjectFactory objectFactory = new ObjectFactory();
    JAXBElement<String> code = objectFactory.createProdRequestCode("test");
    CWDocProdRequest doc = jaxbElement.getValue();
    doc.setCode(code);

    return doc;
}

}

    @Configuration
    @EnableBatchProcessing
    public class XMLReaderConfiguration {

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;


@Bean
public BatchCWAutoProcessor processor() {
    return new BatchCWAutoProcessor();
}

@Bean(destroyMethod="")
public StaxEventItemReader <JAXBElement<ProdRequest>> reader(){
    StaxEventItemReader<JAXBElement<ProdRequest>> reader = new StaxEventItemReader<JAXBElement<ProdRequest>>();
    reader.setResource(new ClassPathResource("policy.xml"));
    reader.setFragmentRootElementName("ProdRequest");




    Jaxb2Marshaller xmlMarshaller = new Jaxb2Marshaller();

    ObjectFactory objectFactory = new ObjectFactory();

    xmlMarshaller.setClassesToBeBound(new Class[] { 
                    //auto generated JAXB classes
           });

    xmlMarshaller.setSupportJaxbElementClass(true);


    reader.setUnmarshaller(xmlMarshaller);



    return reader;
}

@Bean(destroyMethod="")
public StaxEventItemWriter<ProdRequest> writer(){
    StaxEventItemWriter<ProdRequest> writer = new StaxEventItemWriter<ProdRequest>();
    String exportFilePath = "C:/springBatchTest.xml";
    writer.setResource(new FileSystemResource(exportFilePath));
    writer.setRootTagName("ProdRequest");


    Jaxb2Marshaller xmlMarshaller = new Jaxb2Marshaller();

    xmlMarshaller.setClassesToBeBound(new Class[] {
                //list auto generated JAXB classes here
           });

    xmlMarshaller.setSupportJaxbElementClass(true);

    writer.setMarshaller(xmlMarshaller);
    return writer;
}

@Bean
public Step step1(){
    return stepBuilderFactory.get("step1")
            //.<ProdRequest, ProdRequest> chunk(10)//changed this to the line below
            .<JAXBElement<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();               
}
    }
Kachopsticks
  • 125
  • 1
  • 13