使用 JAX-RPC 发送隐式 SOAP 头
可以使现有的 Java™ API for XML-based RPC (JAX-RPC) Web Service 客户机在隐式 SOAP 头中发送值。通过修改客户机代码来发送隐式 SOAP 头,可以在出局 web service 请求中发送特定信息。
开始之前
- 在 Web 服务描述语言 (WSDL) 文件的绑定中声明为 SOAP 头的消息部件,但消息定义未由 WSDL 文件中的 portType 元素引用。
- 未包含在 WSDL 文件中的元素。
处理程序和服务端点可以通过使用带附件的 SOAP API for Java (SAAJ) 数据模型来处理隐式或显式 SOAP 头。
不能处理受保护的 SOAP 头。对于客户机应用程序,如果 SOAP 头声明为受其自有组件(例如,Web Service 安全性)保护,那么该 SOAP 头不可访问。如果尝试处理受保护的 SOAP 头,那么会发生异常。
关于此任务
客户机应用程序对 Stub 或 Call 对象设置属性,以发送和接收隐式 SOAP 头。
过程
结果
具有已配置为发送隐式 SOAP 头的 JAX-RPC Web service 客户机。
示例
以下编程示例说明了如何在 Web Service 请求和响应中发送两个请求 SOAP 头和接收一个响应 SOAP 头:
1 //Create the request and response hashmaps.
2 HashMap requestHeaders=new HashMap();
3 HashMap responseHeaders=new HashMap();
45 //Add "AtmUuid1" and "AtmUuid2" to the request hashmap.
6 requestHeaders.put(new QName("com.rotbank.security", "AtmUuid1"),
7 "<AtmUuid1 xmlns=\"><uuid>ROTB-0A01254385FCA09</uuid></AtmUuid1>");
8 requestHeaders.put(new QName("com.rotbank.security", "AtmUuid2"),
9 ((IBMSOAPFactory)SOAPFactory.newInstance()).createElementFromXMLString(
10 "x:AtmUuid2 xmlns:x=\"com.rotbank.security\"><x:uuid>ROTB-0A01254385FCA09
</x:uuid><x:AtmUuid2>"));
11
12 //Add "ServerUuid" to the response hashmap.
13 //If "responseHeaders" is empty, all the SOAP headers are
14 //extracted from the response message.
15 responseHeaders.put(new QName("com.rotbank.security","ServerUuid"), null);
1617 //Set the properties on the Stub object.
18 stub.setProperty(Constants.REQUEST_SOAP_HEADERS.requestHeaders);
19 stub.setProperty(Constants.RESPONSE_SOAP_HEADERS.responseHeaders);
2021 //Call the operationon the Stub.
22 stub.foo(parm2, parm2);
2324 //Retrieve "ServerUuid" from the response hashmap.
25 SOAPElement serverUuid =
26 (SOAPElement) responseHeaders.get(new QName("com.rotbank.security","ServerUuid"));
2728 //Note: "serverUuid" now equals a SOAPElement object that represents the
29 //following code:
30//"<y:ServerUuid xmlns:y=\"com.rotbank.security\"><:uuid>ROTB-0A03519322FSA01
</y:uuid></y:ServerUuid.");
在第 2 至 3 行上,创建了用于请求和响应 SOAP 头的新的 HashMap。
在第 6 至 10 行上,将 AtmUuid1 和 AtmUuid2 头元素添加到请求 HashMap。
在第 15 行上,将 ServerUuid 头元素名称随空值一起添加到响应 HashMap。
在第 18 行上,将请求 HashMap 设为 Stub 对象的属性。这导致 AtmUuid1 和 AtmUuid2 头被添加到每条请求消息,这样的请求消息与对 Stub 对象调用的操作相关联。
在第 19 行上,将响应 HashMap 设为 Stub 对象的属性。这导致 ServerUuid 头被从每条响应消息中抽取出来,这样的响应消息与对 Stub 对象调用的操作相关联。
在第 22 行上,对 Stub 对象调用 Web Service 操作。
在第 25 至 26 行上,从响应 HashMap 中检索到 ServerUuid 头。Web Service 引擎将该头从响应消息中抽取出来,然后插入到 HashMap 中。