Customizing Java packages JAXB wsimport

maqjav picture maqjav · Sep 19, 2014 · Viewed 21.3k times · Source

I'm trying to generate a client with maven and jaxb from a wsdl file with 2 schemas inside and some elements with the same name from different schemas

When I try to execute the compilation I'm getting the next error:

Two declarations cause a collision in the ObjectFactory class.

WSDL schemas:

<wsdl:types>
    <schema targetNamespace="http://ws.services" xmlns="http://www.w3.org/2001/XMLSchema">...</schema>
    <schema targetNamespace="http://ws.models" xmlns="http://www.w3.org/2001/XMLSchema">...</schema>
</wsdl:types>

I tried renaming the elements that produce the error, but then my spring client receive the correct SOAP message, but it doesn't populate the response object properly (all its attributes are null). I guess the problem might come from renaming the response classes, so that's why I'm trying to generate different packages keeping the original name of all the classes.

In order to do that, I wrote the next bindings file but I don't know what I'm doing wrong that it is not working.

bindings.xml file:

<?xml version="1.0" encoding="UTF-8"?>  
<jaxb:bindings version="2.1"  
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"  
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"  
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"  
    xmlns:xs="http://www.w3.org/2001/XMLSchema" >  

<jaxb:bindings schemaLocation="mywsdl.wsdl#types?schema1"
    node="/xs:schema[@targetNamespace='http://ws.services']">  
        <jaxb:schemaBindings>  
            <jaxb:package name="package1" />  
        </jaxb:schemaBindings>  
</jaxb:bindings>  

<jaxb:bindings schemaLocation="mywsdl.wsdl#types?schema2"
    node="/xs:schema[@targetNamespace='http://ws.models']">  
        <jaxb:schemaBindings>  
            <jaxb:package name="package2" />  
        </jaxb:schemaBindings>  
</jaxb:bindings> 

</jaxb:bindings>  

My configuration part in the maven file is the next, just in case it is useful:

<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.3</version>
<executions>
    <execution>
        <goals>
            <goal>wsimport</goal>
        </goals>
    </execution>
</executions>
<configuration>
    <wsdlLocation>wsdl/mywsdl.wsdl</wsdlLocation>
    <wsdlDirectory>src/main/resources/wsdl</wsdlDirectory>
    <wsdlFiles>
        <wsdlFile>mywsdl.wsdl</wsdlFile>
    </wsdlFiles>
    <bindingDirectory>src/main/resources/wsdl</bindingDirectory>
    <bindingFiles>
        <bindingFile>bindings.xml</bindingFile>
    </bindingFiles>
    <packageName>original.package</packageName>
    <sourceDestDir>${basedir}/src/main/java</sourceDestDir>
</configuration>

When I compile with this bindings files, the same error appears. So I think that maybe it isn't right.

Do you find any mistakes?

Thanks.

Answer

headcr4sh picture headcr4sh · Sep 19, 2014

From my experience it is best to create 2 binding files (one for each WSDL file). Update your pom.xml accordingly and make sure that the root element of the binding files is jaxws:bindings (and not jaxb:bindings!)

Some hints:

  1. Make sure to set the "wsdlLocation" attribute correctly! It must point to the WSDL file using a relative path!
  2. The jaxws:package determines the package that will be used for the generated service classes. (the stuff annotated with @WebService)
  3. Enable or disable wrapperStyle and asyncMapping as you wish. ;-)

Example binding file for "package1":

<?xml version="1.0" encoding="UTF-8"?>

    <jaxws:bindings xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
                    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
                    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
                    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                    xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    wsdlLocation="mywsdl.wsdl"
                    version="2.0">

        <jaxws:package name="package1"/>
        <jaxws:enableWrapperStyle>false</jaxws:enableWrapperStyle>
        <jaxws:enableAsyncMapping>true</jaxws:enableAsyncMapping>

        <jaxws:bindings node="//wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='http://ws.services']">
            <jaxb:schemaBindings>
                <jaxb:package name="package1"/>
            </jaxb:schemaBindings>
        </jaxws:bindings>

    </jaxws:bindings>