SpringBatch Jaxb2Marshaller:类和xml属性的不同名称

时间:2014-08-21 15:47:25

标签: java xml spring jaxb spring-batch

我尝试读取xml文件作为spring批处理的输入:

Java类:

package de.example.schema.processes.standardprocess;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Process", namespace = "http://schema.example.de/processes/process", propOrder = {
    "input"
})
public class Process implements Serializable
{

    @XmlElement(namespace = "http://schema.example.de/processes/process")
    protected ProcessInput input;

    public ProcessInput getInput() {
        return input;
    }

    public void setInput(ProcessInput value) {
        this.input = value;
    }

}

SpringBatch dev-job.xml:

<bean id="exampleReader" class="org.springframework.batch.item.xml.StaxEventItemReader" scope="step">
    <property name="fragmentRootElementName" value="input" />
    <property name="resource"
        value="file:#{jobParameters['dateiname']}" />
    <property name="unmarshaller" ref="jaxb2Marshaller" />
</bean>

<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>de.example.schema.processes.standardprocess.Process</value>
            <value>de.example.schema.processes.standardprocess.ProcessInput</value>
            ...
        </list>
    </property>
</bean>

输入文件:

<?xml version="1.0" encoding="UTF-8"?>
<process:process xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:process="http://schema.example.de/processes/process">
    <process:input>
        ...
    </process:input>
</process:process>

它会触发以下异常: [javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schema.example.de/processes/process", local:"input"). Expected elements are <<{http://schema.example.de/processes/process}processInput>] at org.springframework.oxm.jaxb.JaxbUtils.convertJaxbException(JaxbUtils.java:92) at org.springframework.oxm.jaxb.AbstractJaxbMarshaller.convertJaxbException(AbstractJaxbMarshaller.java:143) at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:428)

如果我在xml中更改它可以正常工作。不幸的是,我既不能改变xml也不能改变java类。

是否有可能让Jaxb2Marshaller映射元素&#39;输入&#39;到班级&#39; ProcessInput&#39;?

1 个答案:

答案 0 :(得分:1)

我不相信JAXB允许这样做。 JAXB是一个绑定API,因此它不能提供很多自定义方式。话虽这么说,您可以使用XStream并根据需要提供别名,允许您根据需要自定义XML到对象的映射。您可以在此处查看XStream示例:https://github.com/spring-projects/spring-batch/blob/master/spring-batch-samples/src/main/resources/jobs/iosample/xml.xml