As I described here I am using JAXWS’ wsimport function to generate a Webservice interface and the JAXB objects for my application. The generated JAXB objects are also used as parameters for some remote EJB calls. Therefore I had to make the JAXB objects serializable. But the question is how to do this since these classes are generated.
Well, in these situations Google is your friend. After a short search I found out to perform the following steps:
- Create a binding file and add the xjc:serializable option to it.
- Setup the JAX-WS Maven plugin like this:
It will look like this:
<?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" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" elementFormDefault="qualified" attributeFormDefault="unqualified" jaxb:extensionBindingPrefixes="xjc" jaxb:version="2.1"> <xs:annotation> <xs:appinfo> <jaxb:globalBindings> <xjc:serializable /> </jaxb:globalBindings> </xs:appinfo> </xs:annotation> </xs:schema>
I name the file ‘jaxb-bindings.xml’ and added it to the resources directory of my Maven project.
... <!-- Plugin to generate JAXB classes and webservice interface --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxws-maven-plugin</artifactId> <version>1.10</version> <executions> <execution> <goals> <goal>wsimport</goal> </goals> </execution> </executions> <configuration> <wsdlDirectory>src/main/resources/wsdl</wsdlDirectory> <sourceDestDir>${basedir}/generated/src/main/java</sourceDestDir> <bindingFiles> <bindingFile>${basedir}/src/main/resources/jaxb/jaxb-bindings.xml</bindingFile> </bindingFiles> </configuration> <dependencies> <dependency> <groupId>javax.jws</groupId> <artifactId>jsr181-api</artifactId> <version>1.0-MR1</version> </dependency> </dependencies> </plugin> ...
This configuration looks similar to the one I had except of course the ‘bindingFiles’ part.
That’s it! If you generate the sources again by running ‘mvn clean install’ and check the generated files you will see that now they implement the Serializable interface.