Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating unique serializable id for each of the generated classes in JAXB

I am using ant wsimport to generate client stub from the wsdls. Also, I would like to generate client classes that implements Serializable. I would like to generate a different serialVersionUID for each class. I tried with the binding file that was shown below. But its generating same serialVersionUID for all the classes. Is there any way I can give my own serialVersionUID to each class?

<wsimport xendorsed="true" binding="binding.xml" debug="true" keep="true" 
verbose="false"  sourcedestdir="${generated}" wsdl="${src}${wsdl.file}"
wsdlLocation="${wsdl.file}">
</wsimport>

binding configuration

<bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <globalBindings>   
        <serializable uid="1" />        
    </globalBindings>    
</bindings>
like image 798
javageek Avatar asked Dec 15 '14 05:12

javageek


2 Answers

Just for the record, there is no way to generate a unique serialVersionUID for each generated class because it doesn't make sense to do so.

Let me explain : A serialVersionUID represents a version of your class at a particular point in time. If you modify your class, your serialVersionUID should change. So when the JDK deserialize objects of the same class, it knows to which version of your class to deserialize it to.

In the case of JAXB, since you generate all your classes at once every time it doesn't make sense to version all the classes individually. Simply because they can only change as a group. (Unless you take them out of your target folder..)

I hope that makes a little bit more sense.

like image 198
Tinou Avatar answered Oct 24 '22 11:10

Tinou


This is the binding file we use, which does the trick for us.

<xs:schema elementFormDefault="qualified" version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:version="2.0"
jaxb:extensionBindingPrefixes="xjc">
<xs:annotation>
    <xs:appinfo>
        <jaxb:globalBindings>
            <xjc:serializable />
        </jaxb:globalBindings>
    </xs:appinfo>
</xs:annotation>

like image 21
Cube Avatar answered Oct 24 '22 10:10

Cube