java - 如何在现有类型中声明或定义通用 xsd 类型。

标签 java xml xsd jaxb

我有一个基于 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 元素定义中声明这一点?

谢谢

最佳答案

为什么不在有效负载中声明所有可能的类型?像这样:

<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"/>

关于java - 如何在现有类型中声明或定义通用 xsd 类型。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11811279/

相关文章:

java - Spring类路径组件扫描

java - 当我使用 Java 8 Stream.of 原始类型时,结果很困惑

xml - 415 不支持的媒体类型 HTTP

java - WSDL 包含中的 XSD 文件路径错误

c# - SQLXML BulkLoader 没有抛出任何错误但没有插入数据

c# - 关闭/忽略 xsd 代码生成中指定的字段后缀

java - 仅在重启时启动 Activity

xml - Ellipsize 不适用于自定义 listView 中的 textView

xml - XSL 副本中的 XPath 祖先和后代

java - 如何从性能日志计算页面加载时间、domContentLoaded 时间(即公式)?