【发布时间】:2021-10-26 10:57:47
【问题描述】:
我有一个简单的项目编写器,如下所示:
return new StaxEventItemWriterBuilder<AddSubscriptionXml>()
.version("1.0")
.rootTagName("BePostSubscriptionDistribution")
.name("optInSumoItemWriter")
.rootElementAttributes(rootAttrs)
.resource(new FileSystemResource(sumoSavePath + "/opt_ins_add.xml"))
.marshaller(jaxb2Marshaller)
.build();
我的编组器具有以下属性:
@Bean
@Scope(SCOPE_PROTOTYPE)
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setMarshallerProperties(getMarshallerProps());
marshaller.setClassesToBeBound(AddSubscriptionXml.class, ModSubscriptionXml.class, DelSubscriptionXml.class);
return marshaller;
}
private Map<String, String> getMarshallerProps() {
return new HashMap<>(){{
put(Marshaller.JAXB_ENCODING, UTF_8.toString());
put(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
put("com.sun.xml.bind.xmlHeaders", "<!DOCTYPE BePostSubscriptionDistribution SYSTEM \"..\\BePostSubscriptionDistribution.dtd\">");
}};
}
奇怪的是,StaxEventItemWriter 完全忽略了编组器属性。
首先,输出看起来像废话,没有缩进。 但更重要的是,我缺少 DocType 行:
结果(自己格式化):
<?xml version="1.0" encoding="UTF-8"?>
<BePostSubscriptionDistribution TimeDateStamp="2021-08-26 13:03:44.761" identification="addSubscription">
<AddMagazineSubscription>
<SubscriptionNumber EditionCode="EDITION_CODE_1">id__19860702 064M81</SubscriptionNumber>
...
期望:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE BePostSubscriptionDistribution SYSTEM "..\BePostSubscriptionDistribution.dtd">
...
知道如何解决这个问题吗?大量时间浪费在本应微不足道的事情上。
注意:在调试模式中,我看到当传递给 ItemWriter 时,marshaller 属性已正确设置。
注意:我在这里打开了一个 Github 问题: https://github.com/spring-projects/spring-batch/issues/3982
【问题讨论】:
标签: java spring-batch stax