Make Serializable JAX-WS clients with Maven2

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:

  1. Create a binding file and add the xjc:serializable option to it.
  2. 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.

  3. Setup the JAX-WS Maven plugin like this:
  4. ...
    <!-- 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.

About Pascal Alma

Pascal is a senior IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery Swift/iOS
This entry was posted in Maven, Web Service and tagged , . Bookmark the permalink.