简体   繁体   中英

Get rid of JAXBElement from jaxb-generated code?

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

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 ie, 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 ie, class name by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM