精彩的人生

好好工作,好好生活

BlogJava 首页 新随笔 联系 聚合 管理
  147 Posts :: 0 Stories :: 250 Comments :: 0 Trackbacks
CSDN - 专家门诊 - Java J2SE / 基础类问题

回复人: panq(漫随天外) ( ) 信誉:100 2001-10-25 13:40:12Z 得分:300

简单对象访问协议(SOAP):
用于Internet上的分布式计算,可以实现跨平台跨编程语言的对象访问和方法调用.
它通过HTTP协议实现参数的传输,同时以特定的XML格式表示参数.这样,只需要一个支持H
TTP协议的WEB服务器和一个XML解析器就可以实现简单的SOAP功能.由于成为了标准,所以
会有越来越多的软件支持它.大家若有兴趣的话可以去看SOAP1.1标准.
同时,还有许多SOAP的开发工具,以简化SOAP的开发.随着这些开发工具的完善,我们
甚至可以不用管具体的SOAP格式就可以开发SOAP应用.
下面以APACHE的SOAP包为例,举个例子:
服务器端的Class:
package samples.soap;
public class SOAPCal {
double rate;
public SOAPCal() {
rate = 8.7;
}
public void setRate(double rate) {
this.rate = rate;
}
public Double calculate(int direction, double value) {
Double retVal;
switch(direction) {
case 0:
retVal = new Double(value * rate);
break;
case 1:
retVal = new Double(value / rate);
break;
default:
retVal = null;
}
return retVal;
}
}
客户端Class:
package samples.soap;
import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
public class SOAPCalUser {
public static void main(String[] args) throws Exception {
if (args.length != 3
&& (args.length != 4 || !args[0].startsWith("-"))) {
System.err.println("Usage:");
System.err.println(" java " + SOAPCalUser.class.getName() +
" [-encodingStyleURI] SOAP-router-URL name " +
"(0: dollor to yuan | 1: yuan to dollor) value");
System.exit (1);
}
int offset = 4 - args.length;
String encodingStyleURI = args.length == 11
? args[0].substring(1)
: Constants.NS_URI_SOAP_ENC;
URL url = new URL(args[1 - offset]);
int direction = Integer.parseInt(args[2 - offset]);
double value = Double.parseDouble(args[3 - offset]);
Call call = new Call();
call.setTargetObjectURI("urn:SOAPCal");
call.setMethodName("calculate");
call.setEncodingStyleURI(encodingStyleURI);

Vector params = new Vector();
params.add(new Parameter("direction", int.class, new
Integer(direction), null));
params.add(new Parameter("value", double.class, new Double(value),
null));
call.setParams(params);
Response resp;
try {
resp = call.invoke(url, "");
} catch (SOAPException e) {
System.err.println("Caught SOAPException (" +
e.getFaultCode() + "): " +
e.getMessage());
return;
}
// Check the response.
if (!resp.generatedFault()) {
Parameter ret = resp.getReturnValue();
Object retVal = ret.getValue();
System.out.println(retVal != null ? "\n" + retVal : "I don't
know.");
} else {
Fault fault = resp.getFault();
System.err.println("Generated fault: ");
System.out.println (" Fault Code = " +
fault.getFaultCode());
System.out.println (" Fault String = " +
fault.getFaultString());
}
}
}

package samples.soap;
import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;

public class SOAPCalAdmin {
public static void main(String[] args) throws Exception {
if (args.length != 2
&& (args.length != 3 || !args[0].startsWith("-"))) {
System.err.println("Usage:");
System.err.println(" java " + SOAPCalAdmin.class.getName()
+ " [-encodingStyleURI] SOAP-router-URL name " +
"rate ");
System.exit (1);
}
int offset = 3 - args.length;
String encodingStyleURI = args.length == 11
? args[0].substring(1)
: Constants.NS_URI_SOAP_ENC;
URL url = new URL(args[1 - offset]);
double rate = Double.parseDouble(args[2 - offset]);
Call call = new Call();
call.setTargetObjectURI("urn:SOAPCal");
call.setMethodName("setRate");
call.setEncodingStyleURI(encodingStyleURI);
Vector params = new Vector();
params.add(new Parameter("rate", double.class, new Double(rate),
null));
call.setParams(params);
Response resp;
try {
resp = call.invoke(url, "");
} catch (SOAPException e) {
System.err.println("Caught SOAPException (" +
e.getFaultCode() + "): " +
e.getMessage());
return;
}
// Check the response.
if (!resp.generatedFault()) {
System.out.println("The rate has been changed.");
} else {
Fault fault = resp.getFault();
System.err.println("Generated fault: ");
System.out.println (" Fault Code = " +
fault.getFaultCode());
System.out.println (" Fault String = " +
fault.getFaultString());
}
}
}
发布用的XML文件dd.xml:
<isd:service xmlns:isd="http://xml.apache.org/xml-soap/deployment"
id="urn:SOAPCal">
<isd:provider type="java"
scope="Application"
methods="setRate calculate">
<isd:java class="samples.soap.SOAPCal"/>
</isd:provider>
</isd:service>
这个程序的功能是设置和计算美元/人民币的兑换价格,用
java org.apache.soap.server.ServiceManagerClient
http://sb3server:8080/soap/servlet/rpcrouter deploy dd.xml
把服务器端程序发布到TOMCAT上,然后在客户端用:
java samples.soap.SOAPCalAdmin http://sb3server:8080/soap/servlet/rpcrouter
XX.XX
来设置汇率(1美元兑换多少人民币), 用
java samples.soap.SOAPCalUser http://sb3server:8080/soap/servlet/rpcrouter
(1|0) XX.XX
来换算,其中1和0代表从人民币到美元和从美元到人民币,下一个参数是要换算的钱数.
另外,在http://www.xmethods.com/gettingstarted/apache.html
有一个简单的教程介绍怎样用APACHE的SOAP包写SOAP客户端程序.讲解比较详细.
注意点:
*如果SOAP传回来的参数是一个CLASS,这个CALSS会被序列化(Serializer)为一段XML
代码,在客户端接这个CLASS时,仍然需要一个同名CLASS,而且那些被传递过来的数据的SE
T和GET方法名和参数名要相同.如果是自定义的CLASS,就要做maping声明.声明方法见APA
CHE SOAP包的samples\addressbook\DeploymentDescriptor.xml文件中isd:mappings的
写法.
在客户端调用时要加上
SOAPMappingRegistry smr = new SOAPMappingRegistry();
BeanSerializer beanSer = new BeanSerializer();
smr.mapTypes(/*encodingStyle*/Constants.NS_URI_SOAP_ENC,
new QName(/*Namespace URI*/"urn:xml-soap-address-demo", /*Local
Part*/"address"),
/*Class type*/Address.class, /*java2XMLClassName*/beanSer,
/*xml2JavaClassName*/beanSer);
call.setSOAPMappingRegistry(smr);
*目前不是所有的CLASS都能被序列化,如Hashtable就不行,所以最好用简单的数据类
型,如String,String,Integer,Double及其数组来传递变量,对复杂的参数,可以直接做成
XML Document传递(使用Constants.NS_URI_LITERAL_XML作为EncodingStyleURI),这样就
省得客户端专门做个CLASS来接参数,也方便跨语言的调用.在使用APACHE的SOAP包时,XML
Document在客户端会被转换为XML DOM的Element class.
现在唯一的方法是自己写进行序列化的程序,不过以后也许会有标准的序列化方法.

*CLASS被序列化时CLASS中的静态的变量是不会被加入的来的,这一点要注意.

posted on 2006-03-09 11:23 hopeshared 阅读(3911) 评论(1)  编辑  收藏 所属分类: Java

Feedback

# re: 转:一个soap的例子 2009-06-22 14:52 1123
也不排个版..  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: