第 12 章 使用 HTTP

摘要

HTTP 是 Web 的底层传输。它为端点之间通信提供了一个标准化、强大且灵活的平台。由于这些因素是大多数 WS-* 规范的假设传输,并且是 RESTful 架构不可或缺的。

12.1. 添加基本 HTTP 端点

备选 HTTP 运行时

Apache CXF 支持以下替代 HTTP 运行时实施:

Netty HTTP URL

通常,HTTP 端点使用在类路径(Undertow 或 Netty)中包含哪些 HTTP 运行时。但是,如果 classpath 中同时包含 Undertow 运行时和 Netty 运行时,则需要在希望使用 Netty 运行时而明确指定,因为默认情况下将使用 Undertow 运行时。

如果 classpath 上有多个 HTTP 运行时,您可以通过指定要以下格式的端点 URL 来选择 Undertow 运行时:

netty://http://RestOfURL

有效负载类型

根据您使用的有效负载格式,可通过三种方式指定 HTTP 端点的地址。

  • SOAP 1.1 使用标准化 soap:address 元素。
  • SOAP 1.2 使用 soap12:address 元素。
  • 所有其他有效负载格式都使用 http:address 元素。
注意

从 Camel 2.16.0 发行版本,Apache Camel CXF Payload 支持开箱即用流缓存。

SOAP 1.1

当您通过 HTTP 发送 SOAP 1.1 消息时,必须使用 SOAP 1.1 地址 元素来指定端点的地址。它有一个属性( 位置 ),用于指定端点的地址为 URL。SOAP 1.1 地址 元素在命名空间 http://schemas.xmlsoap.org/wsdl/soap/ 中定义。

例 12.1 “SOAP 1.1 端口元素” 显示用于通过 HTTP 发送 SOAP 1.1 消息 的端口 元素。

例 12.1. SOAP 1.1 端口元素

<definitions ...
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" ...>
  ...
  <service name="SOAP11Service">
    <port binding="SOAP11Binding" name="SOAP11Port">
      <soap:address location="http://artie.com/index.xml">
    </port>
  </service>
  ...
<definitions>

SOAP 1.2

当您通过 HTTP 发送 SOAP 1.2 消息时,必须使用 SOAP 1.2 地址 元素来指定端点的地址。它有一个属性( 位置 ),用于指定端点的地址为 URL。SOAP 1.2 地址 元素在命名空间 http://schemas.xmlsoap.org/wsdl/soap12/ 中定义。

例 12.2 “SOAP 1.2 端口元素” 显示用于通过 HTTP 发送 SOAP 1.2 消息 的端口 元素。

例 12.2. SOAP 1.2 端口元素

<definitions ...
             xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" ... >
  <service name="SOAP12Service">
    <port binding="SOAP12Binding" name="SOAP12Port">
      <soap12:address location="http://artie.com/index.xml">
    </port>
  </service>
  ...
</definitions>

其他消息类型

当您的消息映射到 SOAP 以外的任何有效负载格式时,您必须使用 HTTP 地址 元素来指定端点的地址。它有一个属性( 位置 ),用于指定端点的地址为 URL。HTTP 地址 元素在命名空间 http://schemas.xmlsoap.org/wsdl/http/ 中定义。

例 12.3 “HTTP 端口元素” 显示用于发送 XML 消息 的端口 元素。

例 12.3. HTTP 端口元素

<definitions ...
             xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" ... >
  <service name="HTTPService">
    <port binding="HTTPBinding" name="HTTPPort">
      <http:address location="http://artie.com/index.xml">
    </port>
  </service>
  ...
</definitions>