How to resolve ObjectFactory Collisions in JAVA
In my previous article I have explained how to generate the web service classes.If you have not already read the topic then I would advise you to do so. Moving on, we can sometimes get an error like below
[ERROR] Two declarations cause a collision in the ObjectFactory cl
line 1191 of file:/D:/RAMEEZ/Web%20Services/TeamBpl/TeamBpl3.xsd
[ERROR] (Related to above error) This is the other declaration.
line 981 of file:/D:/RAMEEZ/Web%20Services/TeamBpl/TeamBpl3.xsd
This is a very common issue we face, and the way to resolve this is
to simply write a XML file having a JAXB binding configuration.
Simple Steps for Clear Understanding:
Here are a few simple steps with images to help you fix the issue quickly :)STEP 1:
Analyze the issue.From the above error log we see line number 1191 and 981 have issues in the
XSD Schema file. Lets look into the XSD file. See below.
STEP 2:
So this is happening because the "name" attribute is somehow causing collision when the WSDL file is getting parsed.STEP 3:
To solve this simple open a new file in Notepad(or any text editor you use) and name it something like "conflict.xml".
STEP 4:
Copy it from here.
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings node="/xs:schema" schemalocation="TeamBpl3.xsd">
<jxb:bindings schemalocation="TeamBpl3.xsd">
<jxb:bindings node="./xs:element[@name='MediaUrl']">
<jxb:factorymethod name="RootMediaElement">
</jxb:factorymethod></jxb:bindings>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
STEP 5:
Now run the "wsimport" command with the -b switch. This is called the binding (-b).wsimport [WSDL_FILE.wsdl] -b conflict.xml -keep -verbose
STEP 6:
This will generate the stub classes properly without any issues.
REMEMBER:
When you have multiple collisions then in that case you have to write multiple jxb:bindings for each. Means you have to repeat line number 5 to 11 from STEP 3.Hope this helps!

