WSDLToJava Error: Thrown by JAXB: Two declarations cause a collision in the ObjectFactory class
Update : I migrate from tumblr to Github Page, slightly modify and re-post this article.
This is the English version of my another post.
Problem
While I was testing PlateSpin Recon Web Service API with Apach CXF, the WSDL2Java gave me the following exception : WSDLToJava Error: Thrown by JAXB: Two declarations cause a collision in the ObjectFactory class. at line 263 column 90 of schema file:/home/cwweng/workspace/recon/PerformanceDataCollectionWS.wsdl.
Turns out it was the following section cause the error
<s:complexType name="MonitorFilter">
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="_sumInstances" type="s:boolean" />
<!-- irrelevant -->
<s:element minOccurs="1" maxOccurs="1" name="SumInstances" type="s:boolean" />
<!-- irrelevant -->
</s:sequence>
</s:complexType>
The root cause is that _sumInstances and SumInstances produce the same property
Solutions
There are seems several solutions I found, but none of them gives me a complete solution. Here they are :
Solution 1
Adding -autoNameResolution argument to WSDL2Java. My test shows no effect, and the error remains.
Solution 2
Do not strip _ by using JAXB binding :
<jaxb:bindings
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:s="http://www.w3.org/2001/XMLSchema"
version="1.0">
<jaxb:globalBindings underscoreBinding="asCharInWord"/>
</jaxb:bindings>
After adding -b binding.xml argument, the error message changed to WSDLToJava Error: java.lang.IllegalArgumentException: trying to create the same field twice: sumInstances
Solution 3
Rename _sumInstances to underscoreBinding by JAXWS binding :
<jaxws:bindings version="1.0"
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"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
wsdlLocation="PerformanceDataCollectionWS.wsdl"
xmlns="http://java.sun.com/xml/ns/jaxws"
xmlns:tns1="VOLARISWS">
<jaxws:schemabindings>
<jaxws:bindings node="wsdl:definitions">
<jaxws:bindings node="//s:element[@nam='_sumInstances']">
<jaxb:property name="underscoreSumInstances" />
</jaxws:bindings>
</jaxws:bindings>
</jaxws:schemabindings>
</jaxws:bindings>
The argument is the same as the previous one: -b binding.xml. The error became WSDLToJava Error: Could not find any node with the XPath expression: //wsdl:definitions///s:element[@nam=’_sumInstances’].
WTF !!!
Solution 4
Rename _sumInstances to underscoreBinding by JAXB binding :
<jaxb:bindings
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:s="http://www.w3.org/2001/XMLSchema"
version="1.0">
<jaxb:bindings schemaLocation="file:PerformanceDataCollectionWS.wsdl" node="/s:schema">
<jaxb:bindings node="//s:element[@name='_sumInstances']">
<jaxb:property name="underscoreSumInstances"></jaxb:property>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
The result was WSDLToJava Error: Thrown by JAXB: “file:/PerformanceDataCollectionWS.wsdl” is not a part of this compilation. Is this a mistake for “file:/PerformanceDataCollectionWS.wsdl#types1”? at line 6 column 91 of schema file:/binding.xml.
It’s driving me crazy.
Hint
This post said Won’t Fix ! and we should use JAXWS instead of JAXB (However, JAXWS did not work in my previous test). The post did give me a solution for the error. After adding #types1 to the filename value of the schemaLocation attribute in binding.xml, that is schemaLocation=”file:PerformanceDataCollectionWS.wsdl#types1” , and it worked !
Conclusion
The modified 4th solution plus the hint works