Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing Java packages JAXB wsimport

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.

like image 859
maqjav Avatar asked Sep 19 '14 11:09

maqjav


People also ask

What is Wsimport?

The wsimport command-line tool processes an existing Web Services Description Language (WSDL) file and generates the required artifacts for developing Java™ API for XML-Based Web Services (JAX-WS) web service applications.

What is JAXB binding file?

JAXB is an XML-to-Java binding technology that enables transformation between schema and Java objects and between XML instance documents and Java object instances. JAXB technology consists of a runtime API and accompanying tools that simplify access to XML documents.

What is the use of XJB file?

Generally we create a bindings file with . xjb extension to resolve any conflicts in the WSDL or schema. For example if two elements have the same name and you want to distinguish between them you can rename one by specifying it the bindings file.


1 Answers

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>
like image 153
headcr4sh Avatar answered Sep 18 '22 17:09

headcr4sh