0

I need to generate Java classes from a WSDL file.

I'm using the jaxws-maven-plugin plugin configured in this way:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <version>2.5</version>
    <executions>
        <execution>
            <id>wsimportPhase</id>
            <goals>
                <goal>wsimport</goal>
            </goals>
            <configuration>
                <keep>true</keep>
                <verbose>true</verbose>
                <packageName>my.package.name</packageName>
                <wsdlFiles>
                    <wsdlFile>${basedir}/wsdl/myWSDL.wsdl</wsdlFile>
                </wsdlFiles>
                <wsdlLocation>wsdl/myWSDL.wsdl</wsdlLocation>
                <bindingDirectory>wsdl</bindingDirectory>
                <bindingFiles>
                    <bindingFile>myBinding.xml</bindingFile>
                </bindingFiles>
                <sourceDestDir>${basedir}/src/main/java</sourceDestDir>
            </configuration>
        </execution>
    </executions>
</plugin>

And I have configured (with some difficulties) the JAXB bindings in this way:

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

<jaxws:bindings version="2.1"
    schemaLocation="myWSDL.wsdl" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
    <jaxws:bindings node="wsdl:definitions/wsdl:types/xs:schema/xs:import">
        <jaxb:bindings schemaLocation="importedXSD_1.xsd">
            <jaxb:bindings node="xs:complexType[@name='DuplicatedType']">
                <jaxb:schemaBindings>
                    <jaxb:nameXmlTransform>
                        <jaxb:typeName suffix="Suffix1"/>
                        <jaxb:elementName suffix="Suffix1"/>
                    </jaxb:nameXmlTransform>
                </jaxb:schemaBindings>
            </jaxb:bindings>
        </jaxb:bindings>
        <jaxb:bindings schemaLocation="importedXSD_2.xsd">
            <jaxb:bindings node="xs:complexType[@name='DuplicatedType']">
                <jaxb:schemaBindings>
                    <jaxb:nameXmlTransform>
                        <jaxb:typeName suffix="Suffix2"/>
                        <jaxb:elementName suffix="Suffix2"/>
                    </jaxb:nameXmlTransform>
                </jaxb:schemaBindings>
            </jaxb:bindings>
        </jaxb:bindings>
    </jaxws:bindings>
</jaxws:bindings>

But when I run mvn install, I have the following error:

[INFO] jaxws:wsimport args: [-keep, -s, '/workspace/myproject/src/main/java', -d, '/workspace/myproject/target/classes', -verbose, -encoding, UTF-8, -Xnocompile, -p, my.package.name, -wsdllocation, wsdl/myWSDL.wsdl, -b, 'file:/workspace/myproject/src/main/resources/wsdl/mybinding.xml', "file:/workspace/myproject/src/main/resources/wsdl/myWSDL.wsdl"]
parsing WSDL...

[ERROR] A class/interface with the same name "my.package.name.DuplicatedType" is already in use. Use a class customization to resolve this conflict.

The XPath finds correctly the needed complexType in the xsd, so I don't understand how to fix it.

Alessandro C
  • 3,310
  • 9
  • 46
  • 82

2 Answers2

0

I have found the solution combining the solutions explained in these two questions:

wsimport - how to generate service endpoint classes and JAXB classes in separate projects/folders

JAXB schema to Java Different XmlRootElement name and Class name

I have made the following steps:

  1. I have created two binding files, one for jaxws-binding and the other one for jaxb-binding.

    These are the two files:

jaxws-binding.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings version="2.1"
    schemaLocation="myWSDL.wsdl" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
    <jaxws:bindings node="wsdl:definitions/wsdl:types/xs:schema/xs:import">
    </jaxws:bindings>
</jaxws:bindings>

jaxb-binding.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="1.0"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">   
    <jaxb:bindings schemaLocation="importedXSD_1.xsd" node="xsd:complexType[@name='DuplicatedType']">
        <jaxb:class name="DuplicatedTypeSuffix1"/>
    </jaxb:bindings>
    <jaxb:bindings schemaLocation="importedXSD_2.xsd" node="xsd:complexType[@name='DuplicatedType']">
        <jaxb:class name="DuplicatedTypeSuffix2"/>
    </jaxb:bindings>
</jaxb:bindings>
  1. I have declared the two binding files in the <bindingFiles> tag of the jaxb-maven-plugin in the pom.xml
Alessandro C
  • 3,310
  • 9
  • 46
  • 82
0

I was doing integration with some company and they gave me their WSDL which I had to import into my project.

  1. Running the below would fail due to naming conflict

    wsimport -keep -verbose http://ip:port/path/service?wsdl

  2. I resolved the issue by adding "-XautoNameResolution"

    wsimport -keep -verbose -XautoNameResolution http://ip:port/path/service?wsdl

And that was it to resolve my pain point. Reference (04-08-2021) https://briskwalk.wordpress.com/2012/03/30/a-classinterface-with-the-same-name-is-already-in-use-error-when-generating-stubs-for-net-service-using-jdk-6-wsimport-command/