用 JAX-RPC 发送与接收 SOAP 消息(1)
 
- UID
- 1066743
|

用 JAX-RPC 发送与接收 SOAP 消息(1)
Web 服务的基础之一是互操作性。意思是说 Web服务相互之间用一种标准的格式发送和接收消息。典型的格式就是SOAP。发送 SOAP 消息的方法有很多。
最基本的操作就是对一个 URL 流执行 println ,不过这要求您理解太多有关 SOAP协议的知识,并且要知道某个服务期望收到的 SOAP消息是什么样,发出的又是什么样。我们无法很方便地从服务本身获得这些信息,因此这种方法的可扩展性并不好。
再进一步就是使用 SAAJ API(SOAP with Attachments API for Java),这种技术在 developerWorks中介绍过了。SAAJ API 使您能够在稍微抽象一点的层次上操纵 SOAP 结构,但是依然存在一些对 URL 流执行 println 的问题。
一种更加友好的方法是使用一种系统,这种系统能够将应用程序推到一个比 SOAP 消息传递协议抽象得多的层次上。这种系统可以带来以下好处:
- 应用程序编程人员可以集中精力编写他们的应用逻辑,而不再被SOAP 的严密逻辑困扰。
- 可以使用 SOAP 以外的其他消息传递模式,而应用程序代码只需做微小修改。
基于 XML 的 Java RPC API (Java APIs for XML-based RPC,JAX-RPC)正是这样一种抽象的方法。
JAX-RPC 以及 Barnes & Noble Web服务JAX-RPC 不依赖于 SOAP,而是依赖于 Web 服务描述语言(Web Services Description Language,WSDL)。WSDL以声明性的标准化方式定义了访问 Web 服务的 API。WSDL 可以定义为将 SOAP 绑定到服务上,但是也可以在高于 SOAP 消息的层次上定义一种更加抽象的描述。(有关WSDL 的更多信息,请参阅 )。
前面提到过一篇有关 SAAJ 的文章,其中的例子使用了位于 的 Barnes & Noble Web 服务。XMethods 网站还提供了这个 Web 服务的 WSDL(请参阅 ),请参见清单1。本文的例子中使用到了这个 WSDL。
清单 1. XMethods 上的 Barnes & Noble WSDL1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
| <?xml version="1.0" ?>
<definitions name="BNQuoteService"
targetNamespace="http://www.xmethods.net/sd/BNQuoteService.wsdl"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://www.xmethods.net/sd/BNQuoteService.wsdl">
<message name="getPriceRequest">
<part name="isbn" type="xsd:string" />
</message>
<message name="getPriceResponse">
<part name="return" type="xsd:float" />
</message>
<portType name="BNQuotePortType">
<operation name="getPrice">
<input message="tns:getPriceRequest" name="getPrice" />
<output message="tns:getPriceResponse" name="getPriceResponse" />
</operation>
</portType>
<binding name="BNQuoteBinding" type="tns:BNQuotePortType">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="getPrice">
<soap peration soapAction="" />
<input name="getPrice">
<soap:body
use="encoded"
namespace="urn:xmethods-BNPriceCheck"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output name="getPriceResponse">
<soap:body
use="encoded"
namespace="urn:xmethods-BNPriceCheck"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
</binding>
<service name="BNQuoteService">
<documentation>Returns price of a book at BN.com given an
ISBN number</documentation>
<port name="BNQuotePort" binding="tns:BNQuoteBinding">
<soap:address location=
"http://services.xmethods.net:80/soap/servlet/rpcrouter" />
</port>
</service>
</definitions>
|
在任何 JAX-RPC 应用程序中,您都必须完成以下三件事情:
- 实例化一个 Service 类,这个类在客户端代表了一个 Web 服务。
- 在 Web服务的应用程序中实例化一个代理(可能还要设置该代理)。
- 调用实现Web 服务的应用程序中的操作。
|
|
|
|
|
|