gpt4 book ai didi

java - 获取 java.lang.ClassCastException : javax. xml.bind.JAXBElement cannot be cast to com.[mySchema] 在实现 ItemProcessor 时

转载 作者:行者123 更新时间:2023-11-30 10:15:24 24 4
gpt4 key购买 nike

所以我正在创建我的第一个 Spring 批处理作业。我正在尝试读入 xml,操作一些值,然后将其写出。如果我不实现 org.springframework.batch.item.ItemProcessor 接口(interface),我的工作似乎是解码我的 XML 文档并将其写出来。一旦我实现了我需要做的接口(interface)来操作这些值,我就得到了下面的错误。当我检查输出文件时,它输出 xml 标记、开始的根元素标记,没有其他任何内容。如果我在我的工作的第 1 步中注释掉对我的处理器的调用,那么该文件将写到我的文件目录中。任何帮助将不胜感激,我一直在寻找解决办法,但没有任何运气。我的消息针对多个大型 xsd 进行验证,因此我将它们排除在代码之外。

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

最佳答案

所以最终我在 spring 框架中设置了一个断点,发现在我的文档被解码之后,它实际上返回了一个通用的 JAXBElement,其中包含我期望的文档。当那个 JAXBElement 被传递到我正在实现 Spring ItemProcessor 的 BatchProcessor 类时,错误被抛出,因为我的类不期望文档在 JAXBElement 中。所以为了解决这个问题,我不得不将 BatchProcessor 的参数更改为 JAXBElement。我的修复如下。我删除了一些公司特定的名称和我的 JAXBClasses 列表。希望这对以后的人有所帮助。谢谢!

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

关于java - 获取 java.lang.ClassCastException : javax. xml.bind.JAXBElement cannot be cast to com.[mySchema] 在实现 ItemProcessor 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50456278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com