2

我有一个基于 Java spring 的 Web 服务,它返回许多响应。我想设计响应,使其具有通用性,因为许多字段始终可用,并且调用服务的结果放置在 JAXB 响应中的“有效负载”元素中。为此,我提出了以下响应类型定义:

<xs:element name="serviceResponse">
    <xs:element name="responseStatus" type="xs:int"/>               
    <xs:element name="errorNumber" type="xs:int"/>
    <xs:element name="errorDescription" type="xs:string"/>
    <xs:element name="payLoad" type="?????"/>
</xs:element>

如果该元素的类型可能因服务类型而异,我如何声明 payLoad 元素。每个服务都会生成一个不同的 JAXB 类型,我想将其添加到“PayloadElement”中。例如,这里有几个我期望的示例响应:

客户名单:

<serviceResponse>
    <responseStatus>0</responsesStatus>
    <errorNumber>0</errorNumber>
    <errorDescription>null</errorDescription>
    <payLoad>
        <customers>
            <customer>
                <customerId>2323</customer>
                <customerName>Joe Bloggs</customerName>
                <invoiceNumber>90347347</invoiceNumber>
            </customer>
            <customer>
                <customerId>54</customer>
                <customerName>Dave hewitt</customerName>
                <invoiceNumber>342343</invoiceNumber>
            </customer>
        </customers>
    </payLoad>
</serviceResponse>

订单清单

<serviceResponse>
    <responseStatus>0</responsesStatus>
    <errorNumber>0</errorNumber>
    <errorDescription>null</errorDescription>
    <payLoad>
        <orders>
            <order>

                <orderId>567</orderId>
                <ProductList>
                    <product>
                        <productId>234324</product>
                        <Quantity>5</Quantity>
                    </product>
                    <product>
                        <productId>23434</product>
                        <Quantity>7</Quantity>
                    </product>
                </ProductList>
            </order>
            <order>

                <orderId>34232</orderId>
                <ProductList>
                    <product>
                        <productId>1231</product>
                        <Quantity>5</Quantity>
                    </product>                  
                </ProductList>
            </order>
        </orders>
    </payLoad>
</serviceResponse>

如您所见,有效负载元素可以包含一个 xml 文档,该文档可以是多种类型之一。我如何在 serviceResponse 元素定义中声明它?

谢谢

4

1 回答 1

2

为什么不在有效载荷中声明所有可能的类型?就像是:

<xs:element name="payloadContent">
  <xs:complexType>
    <xs:sequence>
       <xs:element ref="customers" minOccurs="0"/>
       <xs:element ref="orders" minOccurs="0"/>
       <!-- more elements -->
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:element name="payload" ref="payloadContent"/>
于 2012-08-04T19:29:47.703 回答