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

用 JAX-RPC 发送与接收 SOAP 消息(3)
最后,您就可以调用 getPrice 操作了。
清单 5. 调用 getPrice 操作 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
56
57
58
59
| 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");
// Invoke the operation
Object[] actualArgs = {"0672324229"};
Float price = (Float) call.invoke(actualArgs);
System.out.println("price = " + price);
}
catch (Throwable t) {
t.printStackTrace();
}
}
|
然后您运行 java DIITip ,就得到价格: 44.99 。
如果您将这段代码与 SAAJ 那篇文章中的进行比较,就可以看出,您的知识已经从 SOAP 协议向前迈进了一大步。您需要的信息中与 SOAP有关的只有操作风格和编码风格。
正如其名字所暗示的,DII 编程模型是设计用于处理动态服务的。如果服务发生了微小改变,客户机的代码不需要修改太多就能与之相匹配,如果您真的很聪明的话,客户机代码也可以做成动态的,这样就根本不用修改了。
这种模型仍然是相当复杂的,尤其是您必须填充 Call 对象。操作越复杂,您需要提供给 Call 对象的信息也就越复杂。因此如果您知道服务是静态的,并且不会变化,那么您就可以在抽象的食物链上再上升一层。 |
|
|
|
|
|