JAXB and different versions of the class

I wonder if there is any support for the different versions of the class in JAXB. My thoughts on this: xml is a class save object (serialized value), but a class object can be modified due to development (for example, an extension of functionality). Is there a way to find out which version of a class is stored in persistence (read: xml) using only the JAXB API?

It is more convenient for me to store the class version - as it is done in the standard java-serialization mechanism (I mean serialVersionUID) and provide functionality for matching the xml class, even in the case of a different version (for example, adding class version information to the XmlAdapter is stored in xml). By default, if the class version in xml and at runtime is different - throw InvalidClassException.

Example: We have a Test class as follows:

@XmlRootElement
@XmlAccessorType(value = XmlAccessType.FIELD)
public class Test {

    private Long time;

    private Test () {}; 
}

assuming time is UNIX time in milliseconds.

This code was released in production, and this class was stored in the database as xml. The following release shows that there was no good choice to use Long as a temporary representation, and it was changed to Date:

@XmlRootElement
@XmlAccessorType(value = XmlAccessType.FIELD)
public class Test {

    private Date time;

    private Test () {}; 
}

Now there are two ways - either transfer the saved data, or write an xml adapter that will process Long time and apply it to the date.

If we choose the second method, it would be great if the JAXB API provided a version of the class that was stored in xml (provided that if the version of the class is not specified in the version of the class = 0), and we add a new version of the class explicitly (either by annotations or static field):

@XmlRootElement
@XmlAccessorType(value = XmlAccessType.FIELD)
@XmlClassVersion(value = 1)
public class Test {

    private Date time;

    private Test () {}; 
}

or

@XmlRootElement
@XmlAccessorType(value = XmlAccessType.FIELD)
public class Test {

    private static final long xmlSerialVersionUID = 1;

    private Date time;

    private Test () {}; 
}

and JAXB will provide the XmlAdapter as follows:

public abstract class XmlAdapter<ValueType,BoundType> {
    protected XmlAdapter() {}
    public abstract BoundType unmarshal(ValueType v, long classVersion) throws Exception;

    public abstract ValueType marshal(BoundType v, long classVersion) throws Exception;
}

, , . , JAXB xml xml .

. , , .

+5
2

JAXB "Java Architecture for XML Binding", XML-/, Java. article, ,

javax.xml.bind, , unmarshalling, marshalling validation

Java - , serialVersionUID, .

+1

JAXB, xml, java-. java, . Jaxb xml . , . , JIBX, -. .

0

All Articles