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

用 JAX-RPC 发送与接收 SOAP 消息(2)
JAX-RPC DII 客户机应用程序SAAJ API 并不依赖于 WSDL,它只依赖于 SOAP。但是您必须确切地知道这项服务需要的 SOAP 消息是什么样子。JAX-RPC定义了一种动态调用接口(Dynamic Invocation Interface,DII) API,用于访问 Web 服务,严格地讲,这些Web 服务也不依赖于 WSDL(不过您还是必须了解从 WSDL 中获取的一些信息)。然而,您不再需要了解 SOAP 消息的详细内容。DIIJAX-RPC 应用程序包括三个特定的步骤:
- 实例化一个没有 WSDL 的 DII Service 类。
- 实例化一个 DII Call 对象代理,并对其进行设置。
- 调用 Call 对象的 invoke 方法。
实例化一个没有 WSDL 的 DII Service 类这个类不知道 WSDL,但是它必须有一个名字。因此我们使用 WSDL 中定义的服务名。
清单 2. 实例化 DII Service 类 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| import javax.xml.namespace.QName;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
public class DIITip {
public static void main(String args[]) {
try {
// Create a service class with no WSDL information. You
// still must know something about the WSDL, however: the
// service's name.
QName serviceName = new QName(
"http://www.xmethods.net/sd/BNQuoteService.wsdl",
"BNQuoteService");
ServiceFactory factory = ServiceFactory.newInstance();
Service service = factory.createService(serviceName);
}
catch (Throwable t) {
t.printStackTrace();
}
}
|
实例化一个 DII Call 对象代理并对其进行设置DII 代理对象实现 javax.xml.rpc.Call 接口。您可以从刚刚创建的那个服务中获得对 Call 接口的调用。
清单 3. 实例化 Call 对象 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
public class DIITip {
public static void main(String args[]) {
try {
// Create a service class with no WSDL information. You
// still must know something about the WSDL, however: the
// service's name.
QName serviceName = new QName(
"http://www.xmethods.net/sd/BNQuoteService.wsdl",
"BNQuoteService");
ServiceFactory factory = ServiceFactory.newInstance();
Service service = factory.createService(serviceName);
// Now create a dynamic Call object from this service.
// This call object is not yet associated with any
// operation. We'll do that below.
Call call = service.createCall();
}
catch (Throwable t) {
t.printStackTrace();
}
}
|
您现在拥有的这个 Call 对象还不可以用。它并不知道有关操作的任何事情。您需要将以下数据提供给这个对象:
- 操作名称: getPrice
- 输入参数:一个字符串
- 返回值类型:一个浮点数
- 绑定信息:rpc 风格;编码风格
- 访问点: http://services.xmethods.net:80/soap/servlet/rpcrouter
清单 4. 填充 Call 对象 1
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
48
49
50
51
52
53
54
55
| import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.encoding.XMLType;
public class DIITip {
public static void main(String args[]) {
try {
// Create a service class with no WSDL information. You
// still must know something about the WSDL, however: the
// service's name.
QName serviceName = new QName(
"http://www.xmethods.net/sd/BNQuoteService.wsdl",
"BNQuoteService");
ServiceFactory factory = ServiceFactory.newInstance();
Service service = factory.createService(serviceName);
// Now create a dynamic Call object from this service.
// This call object is not yet associated with any
// operation. We'll do that below.
Call call = service.createCall();
// Next, build up the information about the operation...
// The operation name
QName operationName = new QName(
"urn:xmethods-BNPriceCheck",
"getPrice");
call.setOperationName(operationName);
// The input parameter
call.addParameter(
"isbn", // parameter name
XMLType.XSD_STRING, // parameter XML type QName
String.class, // parameter Java type class
ParameterMode.IN); // parameter mode
// The return
call.setReturnType(XMLType.XSD_FLOAT);
// The operation is an RPC-style operation.
call.setProperty(
Call.OPERATION_STYLE_PROPERTY,
"rpc");
// The encoding style property value comes from the
// binding's operation's input clauses encodingStyle
// attribute. Note that, in this case, this call is not
// really necessary - the value we're setting this
// property to is the default.
call.setProperty(
Call.ENCODINGSTYLE_URI_PROPERTY,
"http://schemas.xmlsoap.org/soap/encoding/");
// The target endpoint
call.setTargetEndpointAddress(
"http://services.xmethods.net:80/soap/servlet/rpcrouter");
}
catch (Throwable t) {
t.printStackTrace();
}
}
|
调用 Call对象的 invoke 方法 |
|
|
|
|
|