Red Hat Training

A Red Hat training course is available for Red Hat Fuse

第 32 章 基本数据绑定概念

摘要

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

32.1. 包含和导入架构定义

概述

Apache CXF 支持使用 includeimport schema 标签包含和导入模式定义。通过这些标签,您可以将来自外部文件或资源的定义插入到 schema 元素的范围中。包括和导入之间的基本区别是:

  • 包含在属于所属模式元素相同的目标命名空间的定义中。
  • 导入(导入)包括属于所属 schema 元素的不同目标命名空间的定义。

xsd:include syntax

include 指令具有以下语法:

<include schemaLocation="anyURI" />

引用的 schema (由 anyURI 指定)必须与所属模式属于同一目标命名空间,或者不属于任何目标命名空间。如果引用的 schema 不属于任何目标命名空间,它将在包含的 schema 命名空间中自动采用。

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

例 32.1. 包含另一个 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 示例” 显示已包含的 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 syntax

import 指令具有以下语法:

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

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

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

例 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 “Imported Schema 示例” 显示导入的 schema 文件的内容。

例 32.4. Imported 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 文档中没有引用的 schema 文件中定义的类型包含三个步骤:

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

    重要

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

  3. 将生成的类添加到您的类路径。