user588961
user588961

Reputation: 116

SpringBatch Jaxb2Marshaller: different name of class and xml attribute

I try to read an xml file as input for spring batch:

Java Class:

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>

Input file:

<?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>

It fires the following exception: [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)

If I change to in xml it work's fine. Unfortunately I can change neither the xml nor the java class.

Is there a possibility to make Jaxb2Marshaller map the element 'input' to the class 'ProcessInput'?

Upvotes: 2

Views: 1121

Answers (1)

Michael Minella
Michael Minella

Reputation: 21463

I don't believe JAXB allows this. JAXB is a binding API, so it doesn't provide much in the way of customization. That being said, you can use XStream and provide aliases for what you need, allowing you to customize the mapping of XML to object however you want. You can see an XStream example here: https://github.com/spring-projects/spring-batch/blob/master/spring-batch-samples/src/main/resources/jobs/iosample/xml.xml

Upvotes: 1

Related Questions