搜索

第 32 章 基本数据绑定概念

download PDF

摘要

有很多常规主题适用于 Apache CXF 处理类型映射的方式。

32.1. 包含和导入架构定义

概述

Apache CXF 支持包括和导入 schema 定义,使用 includeimport schema 标签。这些标签允许您将外部文件或资源的定义插入到 schema 元素的范围。包括和导入之间的基本区别是:

  • 包括在属于与封闭 schema 元素相同的目标命名空间的定义中。
  • 导入会导致定义属于来自分隔 schema 元素的不同目标命名空间。

xsd:include 语法

include 指令使用以下语法:

<include schemaLocation="anyURI" />

anyURI 提供的引用模式必须属于与外围模式相同的目标命名空间,或者根本不属于任何目标命名空间。如果引用的模式不属于任何目标命名空间,则在包含该模式时会自动考虑它的命名空间。

例 32.1 “包含 Another Schema 的 Schema 示例” 显示了包含另一个 XML 架构文档的 XML 架构文档示例。

例 32.1. 包含 Another Schema 的 Schema 示例

<definitions targetNamespace="http://schemas.redhat.com/tests/schema_parser"
             xmlns:tns="http://schemas.redhat.com/tests/schema_parser"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns="http://schemas.xmlsoap.org/wsdl/">
  <types>
    <schema targetNamespace="http://schemas.redhat.com/tests/schema_parser"
            xmlns="http://www.w3.org/2001/XMLSchema">
      <include schemaLocation="included.xsd"/>
      <complexType name="IncludingSequence">
        <sequence>
          <element name="includedSeq" type="tns:IncludedSequence"/>
        </sequence>
      </complexType>
    </schema>
  </types>
  ...
</definitions>

例 32.2 “包含的 Schema 示例” 显示包含的架构文件的内容。

例 32.2. 包含的 Schema 示例

<schema targetNamespace="http://schemas.redhat.com/tests/schema_parser"
        xmlns="http://www.w3.org/2001/XMLSchema">
  <!-- Included type definitions -->
  <complexType name="IncludedSequence">
    <sequence>
      <element name="varInt" type="int"/>
      <element name="varString" type="string"/>
    </sequence>
  </complexType>
</schema>

xsd:import 语法

import 指令的语法如下:

<import namespace="namespaceAnyURI"
        schemaLocation="schemaAnyURI" />

导入的定义必须属于 namespaceAnyURI 目标命名空间。如果 namespaceAnyURI 为空或未指定,则导入的模式定义是不限定的。

例 32.3 “Imports Another Schema 的 Schema 示例” 显示了导入另一个 XML 架构的 XML 架构示例。

例 32.3. Imports Another Schema 的 Schema 示例

<definitions targetNamespace="http://schemas.redhat.com/tests/schema_parser"
             xmlns:tns="http://schemas.redhat.com/tests/schema_parser"
             xmlns:imp="http://schemas.redhat.com/tests/imported_types"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns="http://schemas.xmlsoap.org/wsdl/">
  <types>
    <schema targetNamespace="http://schemas.redhat.com/tests/schema_parser"
            xmlns="http://www.w3.org/2001/XMLSchema">
      <import namespace="http://schemas.redhat.com/tests/imported_types"
              schemaLocation="included.xsd"/>
      <complexType name="IncludingSequence">
        <sequence>
          <element name="includedSeq" type="imp:IncludedSequence"/>
        </sequence>
      </complexType>
    </schema>
  </types>
  ...
</definitions>

例 32.4 “导入的 Schema 示例” 显示导入的模式文件的内容。

例 32.4. 导入的 Schema 示例

<schema targetNamespace="http://schemas.redhat.com/tests/imported_types"
        xmlns="http://www.w3.org/2001/XMLSchema">
  <!-- Included type definitions -->
  <complexType name="IncludedSequence">
    <sequence>
      <element name="varInt" type="int"/>
      <element name="varString" type="string"/>
    </sequence>
  </complexType>
</schema>

使用非引用模式文档

使用架构文档中定义的类型,在服务的 WSDL 文档中没有引用这个类型包含三个步骤:

  1. 使用 xsd2wsdl 工具将架构文档转换为 WSDL 文档。
  2. 在生成的 WSDL 文档中使用 wsdl2java 工具为类型生成 Java。

    重要

    您将从 wsdl2java 工具中收到一条警告,表示 WSDL 文档没有定义任何服务。您可以忽略这个警告。

  3. 将生成的类添加到您的 classpath 中。
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.