0

I have an xsd from which I am generating some java code:

<xs:element name="full-account-v2" >
  <xs:complexType>
    <xs:sequence>
        <xs:element name="ban" type="xs:string" />
        <xs:element name="status" type="xs:int" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

This has been working ok and gives me a generated class called FullAccountV2.

I want to use the same complex type elsewhere, so I thought I would create a named complex type in the xsd file, and refer to it like this:

<xs:element name="full-account-v2" type="fullAccountV2Type"/>

The complex type is defined as follows:

<xs:complexType name="fullAccountV2Type">
    <xs:sequence>
        <xs:element name="ban" type="xs:string" />
        <xs:element name="status" type="xs:int" />
    </xs:sequence>
</xs:complexType>

Now suddenly the unmarshaller stops working. The following xml was unmarshalling just fine to a FullAccountV2:

<er-response id="100058" version="2">
  <payload>
   <full-account-v2>
    <ban>BAN_P146058461158163004</ban>
     <status>401</status>
   </full-account-v2>
  </payload>
</er-response>

But now that class no longer appears, and the xml is unmarshalled by jaxb to a JAXBElement with Qname full-account-v2 and declaredType FullAccountV2Type.

ErResponse and Payload are defined elsewhere as:

<xs:complexType name="payloadType">
    <xs:sequence>
        <xs:any processContents="lax" minOccurs="0" />
    </xs:sequence>
</xs:complexType>

I tried defining the type in a separate xml file to the element declaration, but got the same result.

How can such a simple refactor have caused me 8 hours of struggle? What am I doing wrong?

NB this appears to be the inverse of this problem

Community
  • 1
  • 1
mdarwin
  • 1,684
  • 7
  • 28
  • 72
  • 'stops working' is not a helpful phrase. Detail the specific error message or condition – Black Apr 13 '16 at 23:19

1 Answers1

0

Change to

<xs:element name="full-account-v2" type="fullAccountV2" />
<xs:complexType name="fullAccountV2">
    <xs:sequence>
        <xs:element name="ban" type="xs:string" />
        <xs:element name="status" type="xs:int" />
    </xs:sequence>
</xs:complexType>

Now, JAXB schema compiler will generate an artifact by FullAccountV2 name as earlier.

It is also okay to have the name and type have the same value i.e, FullAccountV2. Below change also works fine -

<!-- one is element and other is complexType -->
<xs:element name="fullAccountV2" type="fullAccountV2" />
<xs:complexType name="fullAccountV2">
    <xs:sequence>
        <xs:element name="ban" type="xs:string" />
        <xs:element name="status" type="xs:int" />
    </xs:sequence>
</xs:complexType>

Above schema definition will also generate an artifact FullAccountV2 as earlier. Whatever we assign to type is used by schema compiler to derive artifact name.

This is also noticeable otherwise if you are generating schema xsd using schemagen. For example - for the class definition shown below

@XmlRootElement
public class Pojo {
    public Pojo p;
}

schemagen generates below schema definition

<xs:element name="pojo" type="pojo"/>
<xs:complexType name="pojo">
    <xs:sequence>
      <xs:element name="p" type="pojo" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

Notice, name and type have the same value i.e, class name by default.

Tirath
  • 2,294
  • 18
  • 27
  • Fails with: `javax.xml.bind.MarshalException - with linked exception: [com.sun.istack.internal.SAXException2: unable to marshal type "com.mycompany.global.er.decoupling.binding.response.v2.FullAccountV2" as an element because it is missing an @XmlRootElement annotation]` – mdarwin Apr 27 '16 at 10:15