<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 495,comments - 227,trackbacks - 0

    WSDL 樣式

    對(duì)于剛剛學(xué)習(xí) WSDL 的開(kāi)發(fā)人員而言,最容易混淆的是為文檔選擇樣式。讓我們了解一下不同的選項(xiàng)及其對(duì) WSDL 和 SOAP 文檔的影響。

    編程樣式和編碼

    在 XML 世界中,通常有兩種類(lèi)型的人:將 XML 視為數(shù)據(jù)格式的人和將 XML 視為文檔標(biāo)記的人。通常,二者永遠(yuǎn)對(duì)立。在某種程度上,這種對(duì)立也進(jìn)入了 Web 服務(wù)中。有人將其視為基于 XML 的遠(yuǎn)程過(guò)程調(diào)用 (Remote Procedure Call),還有些人將其視為將 XML 信息從一個(gè)位置傳遞到另一個(gè)位置的方法。

    對(duì) WSDL 而言,這會(huì)在選擇消息的“樣式”時(shí)表現(xiàn)出來(lái)。創(chuàng)建綁定時(shí),可以選擇 Document 樣式(如 Larry 為 Classifieds 服務(wù)就是選擇的此樣式)或 RPC 樣式。這兩個(gè)樣式并不一定就“正確”或“錯(cuò)誤”。但二者均具有自己的優(yōu)點(diǎn)和缺點(diǎn)。

    使用 RPC 樣式時(shí),要執(zhí)行的方法的名稱(chēng)同時(shí)也是有效負(fù)載的根元素的名稱(chēng)。您可能會(huì)說(shuō),等一等。Classifieds WSDL 的結(jié)構(gòu)不就是這樣嗎?是,也不是。之所以說(shuō)是,是因?yàn)楦氐拿Q(chēng)與我們希望服務(wù)執(zhí)行的方法的名稱(chēng)一樣。不過(guò),從某種意義而言,這只是巧合;Larry 是有意這樣設(shè)計(jì)服務(wù)的。

    讓我們看看不同的選擇及其在 WSDL 和 SOAP 方面的表現(xiàn)。

    Document/Literal

    Document/Literal 樣式意味著有效負(fù)載僅包含要向服務(wù)傳遞的實(shí)際數(shù)據(jù)。任何將消息發(fā)送到其目的地所必需的路由均以其他方式來(lái)完成,如通過(guò) soapAction Header 或服務(wù)的特定 URL。

    這樣的消息簡(jiǎn)單而直接(請(qǐng)參見(jiàn)清單 27)。


    清單 27. Document/Literal 消息
                        
    <env:Envelope 
           xmlns:env="http://schemas.xmlSOAP.org/SOAP/envelope/" 
           xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <env:Body>
         <req:content>Vintage 1963 T-Bird...</req:content>
         <req:endDate>4/30/07</req:endDate>
    </env:Body>
    </env:Envelope>
    

    請(qǐng)注意,有效負(fù)載沒(méi)有根元素。在 WSDL 文件中,您直接定義相應(yīng)的元素并將其添加到消息中(請(qǐng)參見(jiàn)清單 28)。


    清單 28. Document/Literal WSDL
                        
    ...
    <wsdl:types>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
             targetNamespace="http://org.apache.axis2/xsd" 
             elementFormDefault="unqualified" 
             attributeFormDefault="unqualified">
    
    ...
        <xs:element type="xs:string" name="content" />
        <xs:element type="xs:string" name="endDate" />
    ...
      </xs:schema>
    
    </wsdl:types>
    
    <wsdl:message name="createNewAdRequestMessage">
      <wsdl:part name="part1" element="ns1:content" />
      <wsdl:part name="part2" element="ns1:endDate" />
    </wsdl:message>
    ...
    <wsdl:portType name="ClassifiedServicePortType">
    
    ...
      <wsdl:operation name="createNewAd">
        <wsdl:input message="tns:createNewAdRequestMessage" />
        <wsdl:output message="tns:createNewAdResponseMessage" />
      </wsdl:operation>
    ...
    </wsdl:portType>
    ...
    <wsdl:binding name="ClassifiedServiceBinding"
     type="tns:ClassifiedServicePortType">
    
      <soap:binding transport="http://schemas.xmlsoap.org/soap/http" 
        style="document" />
    
      <wsdl:operation name="createNewAd">
        <soap:operation soapAction="createNewAd" style="document" />
        <wsdl:input>
          <soap:body use="literal" />
        </wsdl:input>
        <wsdl:output>
          <soap:body use="literal" />
        </wsdl:output>
      </wsdl:operation>
    
    ...
    </wsdl:binding>
    ...
    

    請(qǐng)注意,在 Document 樣式中,屬于有效負(fù)載的所有元素都在模式中具有相應(yīng)的定義。另請(qǐng)注意,此消息具有兩個(gè)截然不同的部分,每個(gè)部分各自引用一個(gè)特定的元素。





    回頁(yè)首


    Wrapped 樣式

    本教程使用 Document/Literal/Wrapped 樣式。此樣式與 Document/Literal 樣式類(lèi)似,不過(guò)其有效負(fù)載具有根元素。這具有包含要執(zhí)行的方法的名稱(chēng)的優(yōu)點(diǎn)(雖然這并不是必需的),同時(shí)也符合 WS-I 基本概要的要求。為了便于理解,下面提供了一個(gè)簡(jiǎn)單的消息(請(qǐng)參見(jiàn)清單 29)。


    清單 29. Document/Literal/Wrapped 消息
                        
    <env:Envelope 
           xmlns:env="http://schemas.xmlSOAP.org/SOAP/envelope/" 
           xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <env:Body>
      <req:createNewAdRequest 
                  xmlns:req="http://daily-moon.com/classifieds/">
         <req:content>Vintage 1963 T-Bird...</req:content>
         <req:endDate>4/30/07</req:endDate>
      </req:createNewAdRequest>
     </env:Body>
    </env:Envelope>
    

    為了保持完整性,下面提供了相關(guān)的 WSDL(請(qǐng)參見(jiàn)清單 30)。


    清單 30. Document/literal/Wrapped WSDL
                        
    ...
    <wsdl:types>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
             targetNamespace="http://org.apache.axis2/xsd" 
             elementFormDefault="unqualified" 
             attributeFormDefault="unqualified">
    ...
        <xs:element name="createNewAdRequest">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="content" />
              <xs:element type="xs:string" name="endDate" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
    ...
      </xs:schema>
    
    </wsdl:types>
    
    <wsdl:message name="createNewAdRequestMessage">
      <wsdl:part name="part1" element="ns1:createNewAdRequest"
     />
    </wsdl:message>
    ...
    <wsdl:portType name="ClassifiedServicePortType">
    
      <wsdl:operation name="createNewAd">
        <wsdl:input message="tns:createNewAdRequestMessage" />
        <wsdl:output message="tns:createNewAdResponseMessage" />
      </wsdl:operation>
    ...
    </wsdl:portType>
    ...
    <wsdl:binding name="ClassifiedServiceBinding"
     type="tns:ClassifiedServicePortType">
    
      <soap:binding transport="http://schemas.xmlsoap.org/soap/http"
     style="document" />
    
      <wsdl:operation name="createNewAd">
        <soap:operation soapAction="createNewAd" style="document"
     />
        <wsdl:input>
          <soap:body use="literal" 
                            namespace="http://ws.apache.org/axis2" />
        </wsdl:input>
        <wsdl:output>
          <soap:body use="literal" 
                            namespace="http://ws.apache.org/axis2" />
        </wsdl:output>
      </wsdl:operation>
    ...
    </wsdl:binding>
    ...
    

    同樣,有效負(fù)載中的所有元素都在模式中定義。





    回頁(yè)首


    RPC/Literal

    RPC 樣式進(jìn)行處理的方式略為不同。進(jìn)入消息中的是 WSDL 的相應(yīng)內(nèi)容,而不是模式中的內(nèi)容。例如,請(qǐng)看以下的 SOAP 消息(請(qǐng)參見(jiàn)清單 31)。


    清單 31. RPC/Literal 消息
                        
    <env:Envelope 
           xmlns:env="http://schemas.xmlSOAP.org/SOAP/envelope/" 
           xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <env:Body>
    
      <req:createNewAdRequest 
                  xmlns:req="http://daily-moon.com/classifieds/">
         <req:content>Vintage 1963 T-Bird...</req:content>
         <req:endDate>4/30/07</req:endDate>
      </req:createNewAdRequest>
     </env:Body>
    </env:Envelope>
    

    消息本身與 Document/Literal/Wrapped 樣式一樣,但 WSDL 大不相同(請(qǐng)參見(jiàn)清單 32)。


    清單 32. RPC/Literal 消息的 WSDL
                        
    ...
    <wsdl:types>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
             targetNamespace="http://org.apache.axis2/xsd" 
             elementFormDefault="unqualified" 
             attributeFormDefault="unqualified">
    ...
      </xs:schema>
    
    </wsdl:types>
    
    <wsdl:message name="createNewAdRequest">
      <wsdl:part name="content
    " element="
    xsd:string" />
      <wsdl:part name="endDate
    " element="
    xsd:string" />
    </wsdl:message>
    
    ...
    <wsdl:portType name="ClassifiedServicePortType">
    
      <wsdl:operation name="createNewAd">
        <wsdl:input message="tns:createNewAdRequest" />
        <wsdl:output message="tns:createNewAdResponseMessage" />
      </wsdl:operation>
    ...
    </wsdl:portType>
    ...
    <wsdl:binding name="ClassifiedServiceBinding"
     type="tns:ClassifiedServicePortType">
    
      <soap:binding transport="http://schemas.xmlsoap.org/soap/http"
     style="document" />
    
      <wsdl:operation name="createNewAd">
        <soap:operation soapAction="createNewAd" style="rpc" />
        <wsdl:input>
          <soap:body use="literal" 
                            namespace="http://ws.apache.org/axis2" />
        </wsdl:input>
        <wsdl:output>
          <soap:body use="literal" 
                            namespace="http://ws.apache.org/axis2" />
        </wsdl:output>
      </wsdl:operation>
    ...
    </wsdl:binding>
    ...
    

    首先,請(qǐng)注意模式中并未定義任何實(shí)際內(nèi)容。相反,message 指定要執(zhí)行的方法的名稱(chēng),而 message part 直接定義每個(gè)元素。另請(qǐng)注意,在 RPC 樣式中,消息部分的名稱(chēng)很重要;此名稱(chēng)是有效負(fù)載內(nèi)的元素的名稱(chēng)。消息類(lèi)型是直接定義的。(當(dāng)然,這意味著您無(wú)法在有效負(fù)載中包含復(fù)雜元素,但由于此樣式用于模擬遠(yuǎn)程過(guò)程調(diào)用,因此這并不是一個(gè)問(wèn)題。)

    portType 中,當(dāng)您指定消息時(shí),可以直接引用與這些元素一起創(chuàng)建的消息。然后,在綁定中,通過(guò)指定 RPC 樣式,可以清楚地確定如何將所有這些內(nèi)容轉(zhuǎn)換為 SOAP 消息。





    回頁(yè)首


    RPC/Encoded

    我們要了解的最后一個(gè)樣式是 RPC/Encoded。此樣式與 RPC/Literal 類(lèi)似,唯一的不同之處在于由 SOAP 消息定義實(shí)際類(lèi)型信息(請(qǐng)參見(jiàn)清單 33)。


    清單 33. RPC/Encoded SOAP 消息
                        
    <env:Envelope 
           xmlns:env="http://schemas.xmlSOAP.org/SOAP/envelope/" 
           xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <env:Body>
       <req:createNewAdRequest 
                  xmlns:req="http://daily-moon.com/classifieds/">
         <req:content xsi:type="xs:string">Vintage 1963 
                                              T-Bird...</req:content>
         <req:endDate
     xsi:type="xs:string">4/30/07</req:endDate>
        </req:createNewAdRequest>
       </env:Body>
    </env:Envelope>
    

    用于定義此消息的 WSDL 與 RPC/Literal 一樣,但向綁定添加了額外的編碼信息(請(qǐng)參見(jiàn)清單 34)。


    清單 34. RPC/Encoded WSDL
                        
    <wsdl:definitions xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
           xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
           xmlns:tns="http://ws.apache.org/axis2"
    
           xmlns:axis2="http://ws.apache.org/axis2"
           xmlns:ns1="http://org.apache.axis2/xsd" 
     
           targetNamespace="http://ws.apache.org/axis2">
    
    <wsdl:types>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
             targetNamespace="http://org.apache.axis2/xsd" 
             elementFormDefault="unqualified" 
             attributeFormDefault="unqualified">
    ...
      </xs:schema>
    
    </wsdl:types>
    
    <wsdl:message name="createNewAdRequest">
      <wsdl:part name="content" element="xsd:string" />
      <wsdl:part name="endDate" element="xsd:string" />
    </wsdl:message>
    ...
    <wsdl:portType name="ClassifiedServicePortType">
    
      <wsdl:operation name="createNewAd">
        <wsdl:input message="tns:createNewAdRequest" />
        <wsdl:output message="tns:createNewAdResponse" />
      </wsdl:operation>
    ...
    </wsdl:portType>
    
    <wsdl:binding name="ClassifiedServiceBinding"
     type="tns:ClassifiedServicePortType">
    
      <soap:binding transport="http://schemas.xmlsoap.org/soap/http" 
    style="rpc" />
    
      <wsdl:operation name="createNewAd">
        <soap:operation soapAction="createNewAd" style="document" />
        <wsdl:input>
          <soap:body use="encoded" 
    encodingStyle=http://schemas.xmlsoap.org/soap/encoding/
      namespace="http://ws.apache.org/axis2" />
        </wsdl:input>
        <wsdl:output>
          <soap:body use="literal" namespace=
    "http://ws.apache.org/axis2" />
        </wsdl:output>
      </wsdl:operation>
    
    ...
    </wsdl:binding>
    ...
    

    手動(dòng)創(chuàng)建 WSDL 文件時(shí),必須自己處理其中的所有內(nèi)容。幸運(yùn)的是,并不一定始終要手動(dòng)進(jìn)行創(chuàng)建。





    回頁(yè)首

    使用 WSDL 生成代碼

    Gene 和 Francis 是該報(bào)社的團(tuán)隊(duì)程序員,借調(diào)自 IT 部門(mén),Classifieds Department 可以隨時(shí)調(diào)請(qǐng)他們處理自己的項(xiàng)目。他們將通過(guò)采用兩種方法來(lái)進(jìn)行生成 WSDL 代碼的工作;Gene 將重點(diǎn)進(jìn)行從 Java 到 WSDL 的代碼生成工作,而 Francis 將從 WSDL 生成 Java 代碼。

    代碼生成的工作原理

    在 WSDL 的早期,最先出現(xiàn)的兩個(gè)應(yīng)用程序是 Java2WSDL 和 WSDL2Java。那么,不能用來(lái)實(shí)現(xiàn)自動(dòng)化功能的自動(dòng)化格式究竟有什么好處呢?當(dāng)然,當(dāng)時(shí)您的選擇是有限的。這些代碼最初是面向 RPC 樣式的,很難自動(dòng)生成帶復(fù)雜有效負(fù)載的系統(tǒng)。

    很快發(fā)展到了目前的階段,這些問(wèn)題得到了很好的解決。Axis2 幾乎可以從任何 WSDL 文檔生成 Java 代碼,也能從 WSDL 生成 Java 類(lèi)。它通過(guò)數(shù)據(jù)綁定實(shí)現(xiàn)了這個(gè)目標(biāo),在數(shù)據(jù)綁定中,XML 結(jié)構(gòu)可以轉(zhuǎn)換為 Java 對(duì)象,反之亦然。此生成過(guò)程將創(chuàng)建代碼,以供隨后進(jìn)行更改、調(diào)整、編譯和運(yùn)行。

    首先,Gene 從 Java 類(lèi)開(kāi)始,使用其生成 WSDL 文檔。然后,F(xiàn)rancis 獲取該文檔,并使用其生成服務(wù)和客戶機(jī)。對(duì)于服務(wù),此生成過(guò)程將創(chuàng)建一個(gè)框架,可以在其中添加自己的代碼,以執(zhí)行希望服務(wù)執(zhí)行的操作。對(duì)于客戶機(jī),它將創(chuàng)建一個(gè)存根,以用于像使用 Java 方法一樣調(diào)用 Web 服務(wù)方法。

    準(zhǔn)備工作

    第一步要確保環(huán)境已經(jīng)準(zhǔn)備就緒。下載 Apache Axis2 的 0.95 版,對(duì)其進(jìn)行解壓縮,然后確保 lib 目錄中的所有其他 *.jar 文件都位于 CLASSPATH 上。

    要運(yùn)行 Web 服務(wù),請(qǐng)安裝 Apache Geronimo(如果尚未安裝)并將其啟動(dòng)。(具體說(shuō)明,請(qǐng)參見(jiàn)第 1 部分。)下載 Axis2 v0.95 War 分發(fā)版并將其復(fù)制到 <GERONIMO_HOME>/deploy 目錄。Geronimo 將自動(dòng)部署 Axis2。





    回頁(yè)首


    Java 類(lèi)

    Gene 從 ClassifiedService 類(lèi)著手,他會(huì)將此類(lèi)作為主服務(wù),同時(shí)也作為進(jìn)行測(cè)試的方法,以確保所有部分均按預(yù)期工作(請(qǐng)參見(jiàn)清單 35)。


    清單 35. ClassifiedService.java
                        
    package org.dailymoon.classifieds;
    
    public class ClassifiedService {
       
       public static int createNewAd(String content, String endDate){
    
          ClassifiedAd newAd = new ClassifiedAd();
          newAd.setEnd(endDate);
          newAd.setContent(content);
          newAd.save();
    
          return 1;
    
       }
    
       public static boolean editExistingAd(ClassifiedAd adToEdit){
    
          //Do stuff with the ad here
          return true;
    
       }
    
    
       public static ClassifiedList getExistingAds(){
    
           ClassifiedAd[] listOfAds = {new ClassifiedAd(), new
     ClassifiedAd(), new ClassifiedAd()};
           ClassifiedList listToReturn = new ClassifiedList(listOfAds);
           return listToReturn;
       }
    
       public static void finalizeIssue(String dateToFinalize){
           //Don't return anything.
           System.out.println(dateToFinalize + " finalized.");
       }
    
       public static void main (String args[]){
    
             ClassifiedService.createNewAd(
               "Eclipse experts needed.  Contact Nick for details.",
                                                       "4/21/2006");
    
             ClassifiedAd adToEdit = new ClassifiedAd();
             adToEdit.setId(1);
             adToEdit.setStart("4/8/2006");
             adToEdit.setEnd("4/30/2006");
             adToEdit.setContent(
                 "Geronimo experts needed.  Contact Nick for details.");
    
             ClassifiedService.editExistingAd(adToEdit);
    
             ClassifiedList adList = ClassifiedService.getExistingAds();
             System.out.println(adList.toString());
    
       }
    
    
    }
    

    此應(yīng)用程序本身相當(dāng)簡(jiǎn)單。它提供了創(chuàng)建新廣告、編輯現(xiàn)有廣告和列出現(xiàn)有廣告的示例。它提供了對(duì) Gene 希望公開(kāi)的四個(gè)方法(createNewAdeditExistingAdgetExistingAdsfinalizeIssue)的基本限制。

    (請(qǐng)確保在生成 WSDL 前將 main 方法注釋掉。此方法不會(huì)有負(fù)作用,但會(huì)生成不必要的額外代碼。)

    此類(lèi)還引用其他兩個(gè)類(lèi) ClassifiedAdClassifiedList。為了生成過(guò)程能夠了解如何將這些對(duì)象構(gòu)建為 XML 結(jié)構(gòu),Gene 將其創(chuàng)建為獨(dú)立的類(lèi)(請(qǐng)參見(jiàn)清單 36)。


    清單 36. ClassifiedAd.java
                        
    package org.dailymoon.classifieds;
    
    public class ClassifiedAd {
    
       private int id;
       private String startDate;
       private String endDate;
       private String content;
    
       public void setId(int newId){
          id = newId;
       }
    
       public void setStartDate(String newStart){
          startDate = newStart;
       }
    
       public void setEndDate(String newEnd){
          endDate = newEnd;
       }
    
       public void setContent(String newContent){
          content = newContent;
       }
    
       public void save(){
           //Save data here
           System.out.println("Ad saved.");
       }
    
    }
    

    同樣,類(lèi)本身并不完整,但已經(jīng)具有了相應(yīng)的結(jié)構(gòu)(請(qǐng)參見(jiàn)清單 37)。


    清單 37. ClassifiedList.java
                        
    package org.dailymoon.classifieds;
    
    public class ClassifiedList {
    
        public ClassifiedAd[] listOfAds;
    
        public ClassifiedList(ClassifiedAd[] newListOfAds){
            listOfAds = newListOfAds;
        }
    
        public ClassifiedAd[] getRawAds(){
             return listOfAds;
        }
    
        public String toString(){
             return "This is a string of results.";
        }
    
    }
    

    此處,Gene 指定 ClassifiedList 包含一個(gè) ClassifiedAd 對(duì)象數(shù)組。

    有了所有這些類(lèi)后,他就可以生成 WSDL 了。





    回頁(yè)首


    生成 WSDL 并定義消息

    創(chuàng)建 WSDL 是一個(gè)簡(jiǎn)單的過(guò)程。Gene 從命令行發(fā)出相應(yīng)的命令,如清單 38 中所示:


    清單 38. 用于生成 WSDL 的命令
                        
    java org.apache.axis2.wsdl.Java2WSDL -cn 
            org.dailymoon.classifieds.ClassifiedService -o
    

    (請(qǐng)注意,此命令應(yīng)該全部在一行輸入。)

    -cn 開(kāi)關(guān)指定形成服務(wù)基礎(chǔ)的類(lèi)。-o 開(kāi)關(guān)指定輸出目錄。如果沒(méi)有出現(xiàn)問(wèn)題,此類(lèi)將以靜默方式執(zhí)行,在輸出文件中生成 ClassifiedService.wsdl 文件。此文件與 Larry 前面生成的文件很相似——它們?cè)O(shè)計(jì)為在相同的服務(wù)上工作——但需要進(jìn)行一些小更改,以調(diào)整生成過(guò)程中采用一般方式命名的項(xiàng)。具體來(lái)說(shuō),不一定始終正確地進(jìn)行參數(shù)轉(zhuǎn)換,將可能必須進(jìn)行重命名。

    以下是生成的 WSDL 文件,其中調(diào)整的部分以黑體顯示(請(qǐng)參見(jiàn)清單 39)。


    清單 39. WSDL 文件
                        
    <wsdl:definitions xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
           xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
           xmlns:tns="http://ws.apache.org/axis2"
           xmlns:ns1="http://org.apache.axis2/xsd" 
           targetNamespace="http://ws.apache.org/axis2">
    
    <wsdl:types>
      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
             targetNamespace="http://org.apache.axis2/xsd" 
             elementFormDefault="unqualified" 
             attributeFormDefault="unqualified">
    
        <xs:element type="ns1:ClassifiedAd" name="ClassifiedAd" />
        <xs:complexType name="ClassifiedAd">
          <xs:sequence>
            <xs:element type="xs:int" name="id" />
            <xs:element type="xs:string" name="content" />
            <xs:element type="xs:string" name="endDate" />
            <xs:element type="xs:string" name="startDate" />
          </xs:sequence>
        </xs:complexType>
    
        <xs:element type="ns1:ClassifiedList" name="ClassifiedList" />
        <xs:complexType name="ClassifiedList">
          <xs:sequence>
            <xs:element minOccurs="0" type="ns1:ClassifiedAd" 
                            name="ClassifiedAd" 
    maxOccurs="unbounded" />
          </xs:sequence>
        </xs:complexType>
    
        <xs:element name="createNewAdRequest">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="content" />
              <xs:element type="xs:string" name="endDate" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
    
        <xs:element name="createNewAdResponse">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:int" name="newAdId" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
    
        <xs:element name="editExistingAdRequest">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="ns1:ClassifiedAd" 
    name="existingAd" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
    
        <xs:element name="editExistingAdResponse">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:boolean" name="
    wasSuccessful"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
    
        <xs:element name="getExistingAdsRequest">
          <xs:complexType />
        </xs:element>
    
        <xs:element name="getExistingAdsResponse">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="ns1:ClassifiedList" 
    name="ClassifiedList" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
    
        <xs:element name="finalizeIssueRequest">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="issueToFinalize"
     />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
    
      </xs:schema>
    
    </wsdl:types>
    
    <wsdl:message name="createNewAdRequestMessage">
      <wsdl:part name="part1" element="ns1:createNewAdRequest" />
    </wsdl:message>
    
    <wsdl:message name="createNewAdResponseMessage">
      <wsdl:part name="part1" element="ns1:createNewAdResponse" />
    </wsdl:message>
    
    <wsdl:message name="getExistingAdsResponseMessage">
      <wsdl:part name="part1" element="ns1:getExistingAdsResponse" />
    </wsdl:message>
    
    <wsdl:message name="editExistingAdRequestMessage">
      <wsdl:part name="part1" element="ns1:editExistingAdRequest" />
    </wsdl:message>
    
    <wsdl:message name="getExistingAdsRequestMessage">
      <wsdl:part name="part1" element="ns1:getExistingAdsRequest" />
    </wsdl:message>
    
    <wsdl:message name="editExistingAdResponseMessage">
      <wsdl:part name="part1" element="ns1:editExistingAdResponse" />
    </wsdl:message>
    
    <wsdl:message name="finalizeIssueRequestMessage">
      <wsdl:part name="part1" element="ns1:finalizeIssueRequest" />
    </wsdl:message>
    
    <wsdl:portType name="ClassifiedServicePortType">
    
      <wsdl:operation name="finalizeIssue">
        <wsdl:input message="tns:finalizeIssueRequestMessage" />
      </wsdl:operation>
    
      <wsdl:operation name="createNewAd">
        <wsdl:input message="tns:createNewAdRequestMessage" />
        <wsdl:output message="tns:createNewAdResponseMessage" />
      </wsdl:operation>
    
      <wsdl:operation name="editExistingAd">
        <wsdl:input message="tns:editExistingAdRequestMessage" />
        <wsdl:output message="tns:editExistingAdResponseMessage" />
      </wsdl:operation>
    
      <wsdl:operation name="getExistingAds">
        <wsdl:input message="tns:getExistingAdsRequestMessage" />
        <wsdl:output message="tns:getExistingAdsResponseMessage" />
      </wsdl:operation>
    
    </wsdl:portType>
    
    <wsdl:binding name="ClassifiedServiceBinding"
     type="tns:ClassifiedServicePortType">
    
      <soap:binding transport="http://schemas.xmlsoap.org/soap/http"
     style="document" />
    
      <wsdl:operation name="createNewAd">
        <soap:operation soapAction="createNewAd" style="document" />
        <wsdl:input>
          <soap:body use="literal" 
                     namespace="http://daily-moon.com/classifieds" />
        </wsdl:input>
        <wsdl:output>
          <soap:body use="literal" 
                     namespace="http://daily-moon.com/classifieds" />
        </wsdl:output>
      </wsdl:operation>
    
      <wsdl:operation name="finalizeIssue">
        <soap:operation soapAction="finalizeIssue" style="document" />
        <wsdl:input>
          <soap:body use="literal" 
                      namespace="http://daily-moon.com/classifieds" />
        </wsdl:input>
      </wsdl:operation>
    
      <wsdl:operation name="editExistingAd">
        <soap:operation soapAction="editExistingAd" style="document" />
        <wsdl:input>
          <soap:body use="literal" 
                       namespace="http://daily-moon.com/classifieds" />
        </wsdl:input>
        <wsdl:output>
          <soap:body use="literal" 
                       namespace="http://daily-moon.com/classifieds" />
        </wsdl:output>
      </wsdl:operation>
    
      <wsdl:operation name="getExistingAds">
        <soap:operation soapAction="getExistingAds" style="document" />
        <wsdl:input>
          <soap:body use="literal" 
                       namespace="http://daily-moon.com/classifieds" />
        </wsdl:input>
        <wsdl:output>
          <soap:body use="literal" 
                       namespace="http://daily-moon.com/classifieds" />
        </wsdl:output>
      </wsdl:operation>
    
    </wsdl:binding>
    
    <wsdl:service name="ClassifiedService">
      <wsdl:port name="ClassifiedServicePort" 
                               
     binding="tns:ClassifiedServiceBinding">
        <soap:address location=
             "http://127.0.0.1:8080/axis2/services/ClassifiedService" />
      </wsdl:port>
    </wsdl:service>
    
    </wsdl:definitions>
    

    其中的大部分更改都只是為了方便或提供易用性;“content”要比“param0”好記得多。其中的兩個(gè)更改——頂部的命名空間和底部的命名空間前綴——是由于 Axis2 的 0.95 版中的兩個(gè)小軟件錯(cuò)誤而需要進(jìn)行的,在您閱讀本文時(shí),可能已經(jīng)不再需要這樣處理了。





    回頁(yè)首


    從 WSDL 生成服務(wù)

    有了 WSDL 文件后,F(xiàn)rancis 就可以使用其生成服務(wù)和客戶機(jī)。(實(shí)際上,F(xiàn)rancis 可以直接使用 Larry 生成的版本。)

    Francis 首先生成服務(wù)器端的代碼,如清單 40 中所示:


    清單 40. 服務(wù)器端代碼
                        
    java org.apache.axis2.wsdl.WSDL2Java -uri ClassifiedService.wsdl 
    -ss -sd -p org.dailymoon.classifieds -d xmlbeans -o service
    

    (同樣,請(qǐng)?jiān)谝恍休斎氪嗣睢#?/p>

    第一個(gè)參數(shù)是 WSDL 文件的 URL。當(dāng)然,您可以使用此應(yīng)用程序訪問(wèn)遠(yuǎn)程文件。第二個(gè)開(kāi)關(guān) -ss 告知應(yīng)用程序生成服務(wù)(而不是客戶機(jī))。-sd 開(kāi)關(guān)告知應(yīng)用程序生成 XML 服務(wù)描述符,從而更便于在生成服務(wù)代碼后進(jìn)行部署。下一個(gè)參數(shù)當(dāng)然是包,其后緊跟數(shù)據(jù)綁定方法。可用的方法有 adbxmlbeansjaxme。最后,為了保持條理清楚,F(xiàn)rancis 將服務(wù)生成到名為 source 的新目錄中。

    所得到的結(jié)果是數(shù)百個(gè)文件。幸運(yùn)的是,您只需要處理其中的一個(gè)即可。





    回頁(yè)首


    實(shí)現(xiàn)服務(wù)

    盡管在本例中,服務(wù)是從 WSDL 文件生成,而該文件本身又是從 Java 類(lèi)生成的,但生成的代碼中并沒(méi)有實(shí)際的邏輯。其中僅包含相應(yīng)的結(jié)構(gòu)。為了讓服務(wù)實(shí)際進(jìn)行一些工作,需要對(duì)框架文件進(jìn)行編輯。

    在此結(jié)構(gòu)中,要處理的文件如清單 41 中所示:


    清單 41. 從 Java 類(lèi)生成的文件
                        
    service\src\org\dailymoon\classifieds\ClassifiedServicePortTypeSkeleton.
    java
    

    此代碼中包含大量注釋?zhuān)?dāng)嘗試自己進(jìn)行配置時(shí),這些注釋就非常有用;但需要對(duì)其進(jìn)行說(shuō)明時(shí),就很容易帶來(lái)干擾。下面是經(jīng)過(guò)清理的版本,并包含為了實(shí)現(xiàn)部分服務(wù)而添加的代碼(請(qǐng)參見(jiàn)清單 42)。


    清單 42. ClassifiedServicePortTypeSkeleton.java
                        
    package org.dailymoon.classifieds;
    
    public class ClassifiedServicePortTypeSkeleton {
    
       public  axis2.apache.org.xsd.CreateNewAdResponseDocument 
                                                          createNewAd   
          (axis2.apache.org.xsd.CreateNewAdRequestDocument param0 ) 
                                                     throws Exception {
    
          //Todo fill this with the necessary business logic
          //throw new  java.lang.UnsupportedOperationException();
    
          System.out.println("New ad requested, to end on " +         
                      param0.getCreateNewAdRequest().getEndDate());
          System.out.println(
                      param0.getCreateNewAdRequest().getContent());
    
          axis2.apache.org.xsd.CreateNewAdResponseDocument 
                                                   responseDoc =
              axis2.apache.org.xsd.CreateNewAdResponseDocument
                                            .Factory.newInstance();
    
          axis2.apache.org.xsd.CreateNewAdResponseDocument
                                   .CreateNewAdResponse response =
                            responseDoc.addNewCreateNewAdResponse();
    
          response.setNewAdId(1138);
          return responseDoc;
    
       }
         
       public  void finalizeIssue
               (axis2.apache.org.xsd.FinalizeIssueRequestDocument param2)
                                                        throws Exception {
    
            //Todo fill this with the necessary business logic
                    
       }
         
       public  axis2.apache.org.xsd.EditExistingAdResponseDocument 
                                                           editExistingAd
            (axis2.apache.org.xsd.EditExistingAdRequestDocument param3) 
                                                        throws Exception {
    
          //Todo fill this with the necessary business logic
          throw new  java.lang.UnsupportedOperationException();
    
       }
         
       public  axis2.apache.org.xsd.GetExistingAdsResponseDocument 
                                                          getExistingAds
            (axis2.apache.org.xsd.GetExistingAdsRequestDocument param5) 
                                                         throws Exception {
    
          //Todo fill this with the necessary business logic
          throw new  java.lang.UnsupportedOperationException();
    
       }
         
    }
    

    在實(shí)際實(shí)現(xiàn)方法前,每個(gè)方法都會(huì)引發(fā) UnsupportedOperationException。為了將數(shù)據(jù)提交到服務(wù),請(qǐng)首先處理參數(shù)和獲得請(qǐng)求本身。這樣,就可以使用 getter 方法來(lái)提取各個(gè)成員了。

    顯然,在實(shí)際的服務(wù)中,您希望進(jìn)行的不僅是輸出文本,而 Francis 僅是為了確保此服務(wù)能夠正常工作。要?jiǎng)?chuàng)建響應(yīng),請(qǐng)從恰當(dāng)?shù)捻憫?yīng)文檔著手,通過(guò)類(lèi)的 Factory 獲得一個(gè)實(shí)例。(類(lèi)本身非常復(fù)雜,包含大量?jī)?nèi)部類(lèi),但有必要了解一下它的結(jié)構(gòu)。)獲得了此文檔后,請(qǐng)創(chuàng)建實(shí)際響應(yīng)本身,并將其添加到此文檔中。

    可以通過(guò)使用 setter 方法在響應(yīng)上設(shè)置值。直接返回響應(yīng)文檔,支持類(lèi)負(fù)責(zé)將其發(fā)送回請(qǐng)求方。





    回頁(yè)首


    部署服務(wù)

    為了部署服務(wù),您需要對(duì)其進(jìn)行編譯,并將其轉(zhuǎn)換為 Axis2 存檔文件。首先編譯和打包服務(wù),如清單 43 中所示。


    清單 43. 打包服務(wù)
                        
    set ANT_HOME=e:\apache-ant-1.6.5
    PATH=%PATH%;%ANT_HOME%\bin;
    set AXIS2_HOME=e:\axis2
    cd service
    ant jar.service
    

    對(duì)于非 Windows 安裝,請(qǐng)對(duì)所用語(yǔ)法進(jìn)行恰當(dāng)調(diào)整,并確保使用的是實(shí)際文件位置。

    此 Ant 任務(wù)將編譯所有相應(yīng)的文件,并創(chuàng)建兩個(gè)存檔文件 ClassifiedService.aar 和 XBeans-packaged.jar,這兩個(gè)文件都位于 build/lib 目錄中。

    要部署服務(wù),請(qǐng)確保 Geronimo 正在運(yùn)行,并將瀏覽器指向清單 44 中所示的位置:


    清單 44. 部署服務(wù)
    http://localhost:8080/axis2/Login.jsp
    

    使用憑據(jù) admin/axis2 登錄,并單擊 Upload Service>Browse。導(dǎo)航到 ClassifiedService.aar 文件,然后單擊 OK。單擊 Upload,以完成此過(guò)程。

    如果單擊 View Services,應(yīng)該看到列出了新服務(wù)。





    回頁(yè)首


    從 WSDL 生成客戶機(jī)存根

    現(xiàn)在剩下任務(wù)就是生成用于訪問(wèn)新服務(wù)的客戶機(jī)。為此,請(qǐng)從命令行執(zhí)行以下命令:


    清單 45. 用于生成客戶機(jī)的命令
                        
    java org.apache.axis2.wsdl.WSDL2Java -uri ClassifiedService.wsdl -p
     org.dailymoon.classifieds -d xmlbeans -o client
    

    同樣,這也是單個(gè)命令,需要在單行中輸入。相應(yīng)的參數(shù)和服務(wù)器端代碼生成幾乎完全相同,不過(guò)不需要服務(wù)描述符。另外,為了保持條理性,F(xiàn)rancis 將新文件放入了獨(dú)立的 client 目錄中。

    這個(gè)類(lèi)應(yīng)該以靜默方式執(zhí)行,在調(diào)用時(shí)會(huì)生成數(shù)百個(gè)文件,但您并不需要直接處理其中的任何文件。





    回頁(yè)首


    創(chuàng)建客戶機(jī)

    代碼生成過(guò)程并不會(huì)實(shí)際創(chuàng)建客戶機(jī),但會(huì)創(chuàng)建一個(gè)類(lèi),可以利用此類(lèi)方便地創(chuàng)建客戶機(jī)。為了簡(jiǎn)化編譯,F(xiàn)rancis 在 client\src\org\dailymoon\classifieds 目錄中創(chuàng)建了一個(gè)名為 Client.java 的新類(lèi)文件。這樣, Ant 就會(huì)選取 .java 文件,并將其與其他源文件一起編譯。

    Francis 添加了清單 46 中的代碼。


    清單 46. 客戶機(jī)
                        
    package org.dailymoon.classifieds;
    
    import axis2.apache.org.xsd.*;
    
    public class Client{
    
       public static void main(java.lang.String args[]){
    
          try{
             ClassifiedServicePortTypeStub stub = 
                  new ClassifiedServicePortTypeStub(null,
         "http://localhost:8080/axis2/services/ClassifiedService");
                
             CreateNewAdRequestDocument cnaDoc = 
                   CreateNewAdRequestDocument.Factory.newInstance();
    
             CreateNewAdRequestDocument.CreateNewAdRequest cnaReq = 
                                  cnaDoc.addNewCreateNewAdRequest();
             cnaReq.setContent("Vintage 1963 T-Bird...");
             cnaReq.setEndDate("7/4/06");
    
             CreateNewAdResponseDocument cnaResDoc = 
                                           stub.createNewAd(cnaDoc);
             System.out.println("New ad ID number:  "+
                    cnaResDoc.getCreateNewAdResponse().getNewAdId());
    
          } catch(Exception e){
             e.printStackTrace();
          }
       }
    }
    

    ClassifiedServicePortTypeStub 類(lèi)表示實(shí)際的服務(wù),您將使用 AXIS_HOME(此處進(jìn)行了省略,以使用缺省設(shè)置)和實(shí)際服務(wù)的位置對(duì)其進(jìn)行實(shí)例化。接下來(lái),通過(guò)引用其 Factory 創(chuàng)建請(qǐng)求文檔,并使用其創(chuàng)建新的 CreateNewAdRequest,而且在此過(guò)程中將其添加到請(qǐng)求文檔中。和在服務(wù)本身中一樣,可以隨后直接使用 setter 方法設(shè)置各個(gè)屬性。

    為了獲得響應(yīng),請(qǐng)使用存根執(zhí)行 createNewAd() 方法,將其作為參數(shù)傳遞給請(qǐng)求文檔。獲得了響應(yīng)文檔或 CreateNewAtResponseDocument 后,可以使用其提取響應(yīng)本身以及該響應(yīng)的屬性。

    現(xiàn)在讓我們運(yùn)行此應(yīng)用程序。





    回頁(yè)首


    運(yùn)行客戶機(jī)

    為了運(yùn)行客戶機(jī),F(xiàn)rancis 首先需要對(duì)其進(jìn)行編譯。執(zhí)行以下步驟(請(qǐng)參見(jiàn)清單 47)。


    清單 47. 編譯客戶機(jī)
                        
    >>set ANT_HOME=e:\apache-ant-1.6.5
    >>PATH=%PATH%;%ANT_HOME%\bin;
    >>set AXIS2_HOME=e:\axis2
    >>cd client
    >>ant jar.client
    Buildfile: build.xml
    
    init:
    
    pre.compile.test:
         [echo] Stax Availability= true
         [echo] Axis2 Availability= true
    
    compile.src:
    
    compile.test:
    
    jar.client:
    
    BUILD SUCCESSFUL
    Total time: 2 seconds
                    

    首先,確保環(huán)境中包含相應(yīng)的環(huán)境變量。(這假定您已經(jīng)安裝了所有的 AXIS2_HOME\lib jar 文件。)接下來(lái),轉(zhuǎn)到 client 目錄(或作為生成過(guò)程輸出的任何目錄)并針對(duì) jar.client 目標(biāo)運(yùn)行 Ant。應(yīng)該看到與斜體所示類(lèi)似的結(jié)果(請(qǐng)參見(jiàn)清單 47)。要運(yùn)行客戶機(jī),首先將 CLASSPATH 修改為包括 resources 目錄以及包含數(shù)據(jù)綁定過(guò)程創(chuàng)建的所有類(lèi)的目錄(請(qǐng)參見(jiàn)清單 48)。


    清單 48. 運(yùn)行客戶機(jī)
                        
    >>set CLASSPATH=E:\WSDLFiles\client\resources\;E:\WSDLFiles\client\bui
    ld\classes\axis2\apache\org\xsd\;%CLASSPATH%
    >>cd build\classes
    >>java org.dailymoon.classifieds.Client 
    

    應(yīng)該看到與清單 49 所示類(lèi)似的結(jié)果:


    清單 49. New ad ID number
                        
    New ad ID number: 1138
    

    本教程的內(nèi)容到此結(jié)束了。





    回頁(yè)首

    posted on 2006-12-29 19:11 SIMONE 閱讀(1355) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): AXISJAVA
    主站蜘蛛池模板: 一级成人生活片免费看| 一个人免费观看日本www视频 | 青青青青青青久久久免费观看| 老妇激情毛片免费| 国产福利免费视频 | 亚洲动漫精品无码av天堂| 亚洲电影一区二区三区| 亚洲六月丁香六月婷婷色伊人 | 中文字幕亚洲精品无码| 精品久久久久亚洲| 182tv免费视频在线观看| 免费观看无遮挡www的视频| 男女交性永久免费视频播放| 亚洲午夜精品一级在线播放放 | 亚洲国产高清国产拍精品| 一区二区三区免费看| 免费A级毛片在线播放| 免费无码一区二区三区蜜桃大| 久久久久久A亚洲欧洲AV冫| 亚洲黄色高清视频| 国产亚洲高清在线精品不卡| 免费萌白酱国产一区二区三区 | 五月天婷亚洲天综合网精品偷| 久久91亚洲人成电影网站| 色偷偷女男人的天堂亚洲网| 日韩一区二区三区免费播放| 91精品国产免费入口| 国产一级淫片a免费播放口之 | 日韩在线播放全免费| 一区二区三区亚洲视频| 亚洲美女一区二区三区| 阿v免费在线观看| 亚洲成人免费电影| 亚洲性日韩精品一区二区三区| 亚洲精品美女视频| 一个人看的www视频免费在线观看 一个人看的免费观看日本视频www | 一本久久免费视频| 国国内清清草原免费视频99| 亚洲熟妇少妇任你躁在线观看无码| 亚洲无圣光一区二区| fc2成年免费共享视频网站|