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。那麼當瀏覽器訪問http://localhost:8080/soap/,你會看見Apache SOAP的歡迎頁面。點擊Run the admin client ,然後 Deploy。你會得到一個螢幕顯示,在那裡你需要填入ID,Scope,Method,Provider Type和JAVA Provider的資訊到表單域中。你能忽略其他所有的表單域,除非你真的需要它們的資訊。我們的“HelloWorld”例子不需要它們,所以,我們填的下面的值:
| ID: |
urn:HelloWorld_SOAPService |
| Scope: |
Application |
| Methods: |
sayHi |
| Provider Type: |
java |
| Java Provider - Provider Class: |
HelloWorld.SOAPService |
| Java 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("/nCall 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中的所有標籤的含義,我建議你花一點時間閱讀 http://www.w3.org/2001/06/soap-envelope 命名空間規範。
我希望本文能夠在你理解SOAP技術上有一定協助。這個技術是簡單的,有趣的,強大的,彈性的。它被用在許多Web應用中,這些應用的數量也在不斷增加。學習SOAP是值得的,至少你要知道它是什麼和它是怎麼運作的。
Translated by caiyi0903(Willpower),2004.1.17