XJB and JAXB binding on simpleType with same name

6.1k Views Asked by At

I'm trying to generate JAVA code with jaxb and spring but I can't get it to work when I have as wsdl file with 2 simpleTypes with the same name but in different namespaces. Does anyone know how I can solve this?

I've been trying out the jaxb:factoryMethod tag but I can't get the syntax correct. But maybe there is a simpler way?

binding.xjb

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:annox="http://annox.dev.java.net"
    xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    jaxb:extensionBindingPrefixes="xjc annox"
    version="2.1"
    targetNamespace="http://com.company/generated"
    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

<jaxb:globalBindings>
</jaxb:globalBindings>

<jaxb:bindings 
         node="/wsdl:definitions/wsdl:types/xs:schema[namespace::*[.='http://com.company/storetaxinformation']]/xs:simpleType[@name='TypeOfTax']" 
         schemaLocation="../../../target/classes/disb.wsdl">
    <annox:annotateClass>@javax.xml.bind.annotation.XmlRootElement(name="TypeOfTaxStoreTax")</annox:annotateClass>
</jaxb:bindings>

<jaxb:bindings
        node="/wsdl:definitions/wsdl:types/xs:schema[namespace::*[.='http://com.company/gettaxinformation']]/xs:simpleType[@name='TypeOfTax']"
        schemaLocation="../../../target/classes/disb.wsdl">
    <annox:annotateClass>@javax.xml.bind.annotation.XmlRootElement(name="TypeOfTaxInfo")</annox:annotateClass>
</jaxb:bindings>
</jaxb:bindings>

error.log

[ERROR] Error while generating code.Location [ file:/C:/wsdl/disb.wsdl{49,52}].
        com.sun.istack.SAXParseException2; systemId: file:/C:/wsdl/disb.wsdl; 
        lineNumber: 49; columnNumber: 52; Two declarations cause a collision in the ObjectFactory class.
2

There are 2 best solutions below

0
On BEST ANSWER

We have solved this now and it was the <generatePackage> element that was causing part of the problem. We also made sure to bind each schema/namespace to its own package. This way the ObjectFactory Classes won't complain.

pom.xml

...
<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.12.3</version>
    <executions>
      <execution>
        <id>generate-sources-servicename</id>
        <goals>
          <goal>generate</goal>
        </goals>
        <configuration>
          <schemaLanguage>WSDL</schemaLanguage>
          <!-- see binding.xjb
          <generatePackage>com.company.generated</generatePackage>
          -->
          <extension>true</extension>
          <forceRegenerate>true</forceRegenerate>
          <bindingIncludes>
...

binding.xjb

  <jaxb:bindings schemaLocation="../../../target/classes/disb.wsdl"
     node="/wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='http://com.company/generated/storetax']" >
     <jaxb:schemaBindings>
         <jaxb:package name="com.company.generated.storetax"></jaxb:package>
     </jaxb:schemaBindings>
</jaxb:bindings>

<jaxb:bindings schemaLocation="../../../target/classes/disb.wsdl"
    node="/wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='http://com.company/storeaccount']" >
    <jaxb:schemaBindings>
        <jaxb:package name="com.company.generated.storeaccount"></jaxb:package>
    </jaxb:schemaBindings>
</jaxb:bindings>
0
On

No, @XmlRootElement won't help. It's about method names in ObjectFactory.

Are you sure it's simple types which cause a collision? XJC is pointing out to methods in the ObjectFactory, so it should be global elments, not simple types

Here's an example of the factoryMethod customization:

<jaxb:bindings 
    schemaLocation="http://schemas.opengis.net/citygml/texturedsurface/1.0/texturedSurface.xsd" 
    node="/xs:schema">
    <jaxb:bindings node="xs:element[@name='_Appearance']">
        <jaxb:factoryMethod name="AAppearance"/>
    </jaxb:bindings>
</jaxb:bindings>

You'll need to find out which elements cause a collision and customize them. Not the simple types.