Search code examples
javaweb-servicesmavensoapwsdl2java

CXF: wsdl2java errors - WSDL contains duplicate names


I am trying to consume a SOAP web service from a 3rd party. I have a maven project and I'm attempting to use cxf-codegen-plugin to run code generation using wsdl2java on a provided WSDL.

The problem with the WSDL is that there is an element and an attribute with the same name within the same complex type. So, I get the following error when trying to run mvn clean install:

Property "SomeId" is already defined. Use <jaxb:property> to resolve this conflict.

After looking around the approach to resolve this seems to be to add a binding file to rename the property name that's giving the error.

My binding file looks like this:

<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

    <jxb:bindings node=".//s:attribute[@name='SomeId']">
        <jxb:property name="anotherId" />
    </jxb:bindings>
</jxb:bindings>

WSDL snippet:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
  xmlns:tns=“namespace” xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
  targetNamespace=“namespace”>
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace=“namespace”>
      <s:element name="GetData">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="request">
              <s:complexType>
                <s:sequence>
                  <s:element minOccurs="0" maxOccurs="1" name="Nbr" type="s:string" />
                </s:sequence>
              </s:complexType>
            </s:element>
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="GetDataResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="GetDataResult">
              <s:complexType>
                <s:sequence>
                  <s:element minOccurs="0" maxOccurs="1" name="ITEM">
                    <s:complexType>
                      <s:sequence>
                        <s:element minOccurs="0" maxOccurs="1" name="Pty">
                          <s:complexType>
                            <s:sequence>
                           <s:element minOccurs="0" maxOccurs="1" name=“SomeId” type="s:string"/> 
                            </s:sequence>
                            <s:attribute name="SomeId" type="s:string" />
                          </s:complexType>

(Note: 'namespace' hides company name.)

And I've pointed my POM to the binding file:

<plugin>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-codegen-plugin</artifactId>
  <executions>
    <execution>
      <id>generate-sources</id>
      <phase>generate-sources</phase>
      <configuration>
        <wsdlOptions>
          <wsdlOption>
            <wsdl>${basedir}/src/main/resources/META-INF/wsdl/a.wsdl</wsdl>
            <wsdlLocation>classpath:META-INF/wsdl/a.wsdl</wsdlLocation>
            <packagenames>
              REMOVED
            </packagenames>
            <extraargs>
              <extraarg>-fe</extraarg>
              <extraarg>jaxws21</extraarg>
              <extraarg>-autoNameResolution</extraarg>
              <extraarg>-exsh</extraarg>
              <extraarg>true</extraarg>
            </extraargs>
            <bindingFiles>
              <bindingFile>${basedir}/src/main/resources/META-INF/wsdl/binding.xsd</bindingFile>
            </bindingFiles>
          </wsdlOption>
        </wsdlOptions>
      </configuration>
      <goals>
        <goal>wsdl2java</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Problem:

When I run 'mvn clean install' with this set-up I get the following error:

XPath evaluation of ".//s:attribute[@name='SomeId']" results in empty target node

I've changed the node path to point at the element and also used a full node path structure e.g. node="wsdl:definitions/wsdl:types...." but I keep getting the same error. Feel like I'm going around in circles here.

I'd be very grateful if someone could see where I'm going wrong and point it out to me, as this is my first time trying to implement something like this.

Thanks in advance.


Solution

  • So I figured this out in the end. Turns out that jaxb:bindings wasn't able to resolve the node path. So I used jaxws to resolve the path and jaxb to update the property name.

    Working bindings file looks like this:

    <jaxws:bindings xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:jaxws="http://java.sun.com/xml/ns/jaxws" xmlns:s="http://www.w3.org/2001/XMLSchema" version="2.1">
      <jaxws:bindings node=".//s:element[@name='SomeId']">
        <jaxb:property name="anotherId" />
      </jaxws:bindings>
    </jaxws:bindings>