我有一个Spring Batch应用程序,我使用StaxEventItemReader作为我的ItemReader。默认情况下,XStream要求我们为每个可能的XML标记声明一个属性,否则会抛出UnknownFieldException异常。有一些方法可以使用Java进行编码,但使用Spring Batch,InputReader似乎没有办法对其进行修改。有没有办法在xml中将字段标记为可选项?
我的bean配置基本上就像这样
<job id="synchronizecustomerData" xmlns="http://www.springframework.org/schema/batch">
<step id="readWritecustomers">
<tasklet>
<chunk reader="customerReader"
processor="customerProcessor"
writer="customerSyncWriter"
commit-interval="1"
skip-policy="alwaysSkip" >
</chunk>
</tasklet>
</step>
</job>
<bean id="customerReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
<property name="fragmentRootElementName" value="customer" />
<property name="resource" ref="inputResource" />
<property name="unmarshaller" ref="customerMarshaller" />
</bean>
<bean id="inputResource" class="org.springframework.core.io.FileSystemResource">
<constructor-arg value="c:/sf/data.xml" />
</bean>
<bean id="customerMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="aliases">
<util:map id="aliases">
<entry key="customer" value="com.company.batchmaster.sf.beans.customer" />
<entry key="name" value="java.lang.String" />
</util:map>
</property>
</bean>
<bean id="customerProcessor" class="org.springframework.batch.item.support.CompositeItemProcessor">
<property name="delegates">
<list>
<ref bean="customerTransformer" />
</list>
</property>
</bean>
<bean id="customerTransformer" class="com.company.batchmaster.sf.chunk.customerTransformer" />
<bean id="customerSyncWriter" class="com.company.batchmaster.sf.chunk.customerSyncWriter" />
我的导入文件如下所示,只需启动并运行
即可<?xml version="1.0" encoding="UTF-8"?>
<records>
<customer xmlns="http://springframework.org/batch/sample/io/oxm/domain">
<name>ABC Dealer</name>
<types>CR</types>
</customer>
</records>
感谢您的帮助。
答案 0 :(得分:0)
我假设Customer
类有属性name
和type
如XmlAttribute.defaultValue()中所述,将其注释为Jaxb guide
不需要此(<entry key="name" value="java.lang.String" />
)别名,因为您正在使用<property name="fragmentRootElementName" value="customer" />