How to create a Java class that implements the Serializable interface from xsd using JAXB?

I would like to introduce caching into an existing Spring project that uses JAXB to publish WebServices. Caching will be performed at the endpoint level. In order for these classes created from XSD using JAXB, you must implement the Serializable interface and override the Object toString() method.

How to instruct the xjc tool using XSD to generate a source with the necessary properties?

+43
java xsd jaxb xjc
Oct 03 '09 at 15:01
source share
6 answers

Serializable

Use xjc:serializable in your custom bindings file to add the java.io.Serializable interface to your classes along with serialVersionUID :

 <?xml version="1.0" encoding="UTF-8"?> <bindings xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xsi:schemaLocation=" http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd" version="2.1"> <globalBindings> <serializable uid="1" /> </globalBindings> </bindings> 

Tostring ()

Use a superclass (see xjc:superClass ), from which all your related classes inherit. This class will not be generated by xjc, so you can create it as you wish (here with the implementation of toString() ):

 <?xml version="1.0" encoding="UTF-8"?> <bindings xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xsi:schemaLocation=" http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd" version="2.1"> <globalBindings> <serializable uid="1" /> <xjc:superClass name="the.package.to.my.XmlSuperClass" /> </globalBindings> </bindings> 
+71
03 Oct '09 at 15:23
source share

This worked for me:

 <?xml version="1.0" encoding="UTF-8"?> <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:version="2.1"> <jaxb:globalBindings> <xjc:serializable uid="1337"/> </jaxb:globalBindings> </jaxb:bindings> 

Hope this helps.

+7
Nov 22 '13 at 11:27
source share

Another way to generate the toString() method is the JAXB2 Basics Plugins . This method looks better because it does not use reflection. An example of how to do this with maven below.

 <build> <plugins> <plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.8.2</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <schemaDirectory> ${project.basedir}/src/main/resources </schemaDirectory> <generateDirectory> ${project.basedir}/src/main/java </generateDirectory> <extension>true</extension> <args> <arg>-XtoString</arg> </args> <plugins> <plugin> <groupId>org.jvnet.jaxb2_commons</groupId> <artifactId>jaxb2-basics</artifactId> <version>0.6.4</version> </plugin> </plugins> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.jvnet.jaxb2_commons</groupId> <artifactId>jaxb2-basics-runtime</artifactId> <version>0.6.4</version> </dependency> </dependencies> 

As a result, you will get such a code.

 public String toString() { final ToStringStrategy strategy = JAXBToStringStrategy.INSTANCE; final StringBuilder buffer = new StringBuilder(); append(null, buffer, strategy); return buffer.toString(); } public StringBuilder append(ObjectLocator locator, StringBuilder buffer, ToStringStrategy strategy) { strategy.appendStart(locator, this, buffer); appendFields(locator, buffer, strategy); strategy.appendEnd(locator, this, buffer); return buffer; } public StringBuilder appendFields(ObjectLocator locator, StringBuilder buffer, ToStringStrategy strategy) { { String theName; theName = this.getName(); strategy.appendField(locator, this, "name", buffer, theName); } { String theBank; theBank = this.getBank(); strategy.appendField(locator, this, "bank", buffer, theBank); } return buffer; } 
+5
Dec 09 '12 at 12:14
source share

I use this code and work for me

 <?xml version="1.0" encoding="UTF-8"?> <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:version="2.1"> <jaxb:globalBindings> <xjc:serializable uid="1"/><!--If you Forgot this line your class did not be serializable--!> </jaxb:globalBindings> </jaxb:bindings> 
+2
Sep 24 '14 at 5:35
source share

to get the Serializable interface, add the following binding information to the xsd file:

<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0" > <xs:annotation> <xs:appinfo> <jaxb:globalBindings optionalProperty="primitive"> <jaxb:serializable/> </jaxb:globalBindings> </xs:appinfo> </xs:annotation> <xs:element name="myXsdElement"> ..... </xs:element> </xs:schema>

+1
Sep 01 '16 at 15:23
source share

And here is an example using CXF 3.1.10. Do not forget to include the compilation group: "org.apache.cxf.xjc-utils", name: "cxf-xjc-runtime", version: "3.1.0"

 <jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" version="2.1"> <jxb:bindings schemaLocation="META-INF/wsdl/xsd2.xsd"> <jxb:globalBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <jxb:serializable uid="1"/> </jxb:globalBindings> 
+1
Feb 28 '17 at 5:08
source share



All Articles