您的位置:首页 > 编程语言 > Java开发

实例讲解:JAVA SOAP技术(2)完

2008-04-25 11:57 609 查看
Very Simple Object Access Protocol with JAVA
By Olexiy & Alexander Prokhorenko

现在,我们来一步步的看看到底发生了什么。在Step 1里,HelloWorldClient将连接一个SOAP RPC Router,请求我们的SOAP Service并将包含我们名字的一个字符串传递给它。该SOAP RPC Router会检查是否它已经部署了这个SOAP Service。如果它被发现是被部署的,那么它将传递数据到这个SOAP Service并调用特定的方法,这个是Step 2。然后SOAP Service方法会被执行,将返回某个字符串值(该值就是SOAP Client的答复或者响应)(Step 3)。在Step4中,SOAP RPC Router将仅仅只是重定向这个数据到SOAP Client。所有在Step1和Step4里传输的数据是通过SOAP Envelope来完成的。正如你所看到的,算法是相当简单的,因此我们只准备关心实际的代码。
首先,我们要创建一个SOAP Service。下面是它的代码,请不要忘记将它放入HelloWorld/ 目录中(必须被包含在你的CLASSPATH中):
1: // SOAPService.JAVA2: package HelloWorld;3: public class SOAPService {4:   public String sayHi(String x) {5:     return("Hello my friend, " + x + "! Glad to see you!");6:   }7: }
添加任何注释也是很容易的。要编译它,只需要用下列命令:
JAVAc SOAPService.JAVA
第二步,一旦我们准备好了SOAP Service,我们需要用SOAP Service Manager来部署它。这个可以通过很多方式来实现,但是为了能让初学SOAP的读者更容易理解SOAP,我提供了一个最容易的方式。我们假设你的Web Server(Tomcat或其他)已经正常运行,并且你已经正确安装了SOAP。那么当浏览器访问SOAP/">http://localhost:8080/SOAP/,你会看见Apache SOAP的欢迎页面。点击Run the admin client ,然后 Deploy。你会得到一个屏幕显示,在那里你需要填入ID,Scope,Method,Provider Type和JAVA Provider的信息到表单域中。你能忽略其他所有的表单域,除非你真的需要它们的信息。我们的“HelloWorld”例子不需要它们,所以,我们填的下面的值:
ID: urn:HelloWorld_SOAPServiceScope: ApplicationMethods: sayHiProvider Type: JAVAJAVA Provider - Provider Class: HelloWorld.SOAPServiceJAVA Provider - Static?No一些注释:ID是我们要从SOAP Client标识我们的SOAP Service的唯一名字。Method包含SOAP Service提供的一系列方法。JAVA Provider-Provider Class是SOAP Service JAVA类的名字。
现在,点击Deploy 按钮,那么你的服务会被部署。再次强调,请注意正确设置CLASSPATH环境变量。然后,你的HelloWorld.SOAPService类能够被找到,并且所有必需的jar包也能被找到。这是个几乎每个人都会犯的普通错误。现在,你能够点击 List ,将会看见你的服务已经被部署进来。恭喜!
最后,让我们来创建一个SOAP Client。代码看起来有点复杂,但是在现实中不会只有这么点长。
1: // HelloWorldClient.JAVA 2: import JAVA.io.*; 3: import JAVA.net.*; 4: import JAVA.util.*; 5: import org.apache.SOAP.*; 6: import org.apache.SOAP.rpc.*; 7: public class HelloWorldClient { 8:   public static void main(String[] arg) throws Exception { 9:     Call c = null;10:     URL url = null;11:     Vector params = null;12:     Response rep = null;13:     String ourName = "Superman";14:     String ourUrn = "urn:HelloWorld_SOAPService";15:     String ourMethod = "sayHi";16:     url = new URL("http://localhost:8080/SOAP/servlet/                       rpcrouter");17:     System.out.println("Passing to our deployed "+ourUrn+"                            our name ("+ourName+"): ");18:     c = new Call();19:     c.setTargetObjectURI(ourUrn);20:     c.setMethodName(ourMethod);21:     c.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);22:     params = new Vector();23:     params.addElement(new Parameter("ourName", String.class,                                         ourName, null));24:     c.setParams(params);25:     System.out.print("and its answer is: ");26:     rep = c.invoke(url, "");27:     if (rep.generatedFault()) {28:       Fault fault = rep.getFault();29:       System.out.println("Call failed!");30:       System.out.println("Code = " + fault.getFaultCode());31:       System.out.println("String = " + fault.getFaultString());32:     } else {33:       Parameter result = rep.getReturnValue();34:       System.out.print(result.getValue());35:       System.out.println();36:     }37:   }38:}
下面我要做一些解释。在第13行,我们设置了我们的名字,这个名字将会传递给SOAP Service。在第14行,我们设置了我们将要调用的服务的ID(service ID),和第15行里设置的服务方法(service method)。有了这个ID,服务能够被部署到SOAP服务管理器(SOAP Service Manager)中。我们没有设置任何其他值,仅仅只用刚才那些基础值就可以正常运作了。你能从SOAP的官方文档上得到相关信息,该文档来自SOAP包中,它们的解释超出了本文的范围。
用以下方式编译这个SOAP Client:
JAVAc HelloWorldClient.JAVA
为了圆满完成它,让我们检查一下针对我们的测试,是否所有事情都准备就绪。Tomcat正在运行,所有的环境变量都正确,SOAP Service被编译和部署,SOAP Client被成功编译。OK,让我们运行它,你将看到这个屏幕:

正如你所看到的,我们的SOAP Client使用SOAP协议成功发送它的名字和接收了一个答复。正如前面所说的,SOAP Service发送和接收的是SOAP envelope。这个是SOAP envelope的源代码。
发送SOAP Service的SOAP Envelope
<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlSOAP.org/                                   SOAP/envelope/"                   xmlns:xsi="http://www.w3.org/2001/                              XMLSchema-instance"                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"><SOAP-ENV:Body><ns1:sayHi xmlns:ns1="urn:HelloWorld_SOAPService"           SOAP-ENV:encodingStyle="http://schemas.xmlSOAP.org/                                   SOAP/encoding/"><ourName xsi:type="xsd:string">Superman</ourName></ns1:sayHi></SOAP-ENV:Body></SOAP-ENV:Envelope>:
SOAP Service接收SOAP Envelope
<?xml version='1.0' encoding='UTF-8'?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlSOAP.org/                                   SOAP/envelope/"                   xmlns:xsi="http://www.w3.org/2001/                              XMLSchema-instance"                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"><SOAP-ENV:Body><ns1:sayHiResponse xmlns:ns1="urn:HelloWorld_SOAPService"                   SOAP-ENV:encodingStyle="http://schemas.xmlSOAP.                                           org/SOAP/encoding/"><return xsi:type="xsd:string">Hello my friend, Superman!                              Glad to see you!</return></ns1:sayHiResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
要理解SOAP Envelope中的所有标签的含义,我建议你花一点时间阅读 SOAP-envelope" target=new>http://www.w3.org/2001/06/SOAP-envelope 命名空间规范。
我希望本文能够在你理解SOAP技术上有一定帮助。这个技术是简单的,有趣的,强大的,弹性的。它被用在许多Web应用中,这些应用的数量也在不断增加。学习SOAP是值得的,至少你要知道它是什么和它是怎么运作的。
Translated by caiyi0903(Willpower),2004.1.17
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: