"Versions" of the JAXB Object?

We create many applications for developers using JAXB and continue to encounter problems that all return to a “version” mismatch between manufacturers and consumers of JAXB objects.

The process didn’t ease the pain, so I was thinking about something in accordance with the version of CORBA object management for JAXB, possibly through a mandatory final field, the values ​​of which must match. As an added bonus, I would like to add the version value as the version of Maven # :-)

All this uses annotations, no xsd.

Thoughts?

Thank.

----- Clarification -----

Think of it as a Serializable serialVersionUID, which is added to the marshal's stream when the object is marshaled and required, and whose value is checked when the object is unarmalized.

Various validation rules can be implemented, but in this case I only want equality. If the current version of Foo is 1.1, and you send me the data for cancellation, whose version is something other than 1.1, I will refuse it.

reference

+5
source share
1 answer

You can do something like the following:

Foo

Add a version field to your root model object.

package forum12218164;

import javax.xml.bind.annotation.*;

@XmlRootElement
public class Foo {

    @XmlAttribute
    public static final String VERSION = "123";

    private String bar;

    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }

}

Demo

Your demo code uses the StAX analyzer to check the version attribute before determining whether it is safe to perform a non-marshal operation:

package forum12218164;

import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        // Create the JAXBContext
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        // Create an XMLStreamReader on XML input
        XMLInputFactory xif = XMLInputFactory.newFactory();
        StreamSource xml = new StreamSource("src/forum12218164/input.xml");
        XMLStreamReader xsr = xif.createXMLStreamReader(xml);

        // Check the version attribute
        xsr.nextTag(); // Advance to root element
        String version = xsr.getAttributeValue("", "VERSION");
        if(!version.equals(Foo.VERSION)) {
            // Do something if the version is incompatible
            throw new RuntimeException("VERSION MISMATCH");
        }

        // Unmarshal for StAX XMLStreamReader
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Foo foo = (Foo) unmarshaller.unmarshal(xsr);

        // Marshal the Object
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }

}

REAL USE

Input.xml / output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo VERSION="123">
    <bar>ABC</bar>
</foo>

INCORRECT USE OF CASE

Input.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo VERSION="1234">
    <bar>ABC</bar>
</foo>

Output

Exception in thread "main" java.lang.RuntimeException: VERSION MISMATCH
    at forum12218164.Demo.main(Demo.java:23)
+6
source

All Articles