07年底的時(shí)候就開(kāi)始學(xué)習(xí)web service,那時(shí)候?qū)W習(xí)Axis1.4并用于項(xiàng)目。這次公司又使用Axis,因?yàn)槭沁z留系統(tǒng),本來(lái)使用的就是Axis。于是對(duì)Web Service和Axis1.4進(jìn)行了一個(gè)學(xué)習(xí)總結(jié)。

1、WebService的概念
Web Service是一個(gè)比較新的分布式服務(wù)組件,本質(zhì)上就是以標(biāo)準(zhǔn)化的方式實(shí)現(xiàn)企業(yè)內(nèi)部各個(gè)不同服務(wù)系統(tǒng)之間的互調(diào)或者集成。實(shí)現(xiàn)以上目的需要三個(gè)元素:
1、服務(wù)B以一種標(biāo)準(zhǔn)化的語(yǔ)言告訴服務(wù)A它能提供什么服務(wù),以及如何調(diào)用它,它的服務(wù)在哪里,這就是Web Service的服務(wù)描述,是what,how和where部分;
2、服務(wù)A要以一種標(biāo)注化的通信消息格式告訴服務(wù)B,它想調(diào)用什么服務(wù),以及相應(yīng)的輸入?yún)?shù)。當(dāng)服務(wù)完成后,B會(huì)以同樣標(biāo)注化的方式返回相應(yīng)的服務(wù)結(jié)果,這就是Web Service的服務(wù)消息的request和response部分;
3、服務(wù)B注冊(cè)到相應(yīng)的地址,以便外部找到,是URL部分。

上面三個(gè)元素實(shí)際上對(duì)應(yīng)于Web Services的三個(gè)組成部分,WSDLSOAP和UDDI。

WSDL1.1和2.0,其中1.1應(yīng)用最為廣泛,支持最多,Axis2.0支持WSDL2.0。

<?xml version="1.0"?>
<definitions name="StockQuote"

targetNamespace
="http://example.com/stockquote.wsdl"
          xmlns:tns
="http://example.com/stockquote.wsdl"
          xmlns:xsd1
="http://example.com/stockquote.xsd"
          xmlns:soap
="http://schemas.xmlsoap.org/wsdl/soap/"
          xmlns
="http://schemas.xmlsoap.org/wsdl/">

    
<types>
       
<schema targetNamespace="http://example.com/stockquote.xsd"
              xmlns
="http://www.w3.org/2000/10/XMLSchema">
           
<element name="TradePriceRequest">
              
<complexType>
                  
<all>
                      
<element name="tickerSymbol" type="string"/>
                  
</all>
              
</complexType>
           
</element>
           
<element name="TradePrice">
              
<complexType>
                  
<all>
                      
<element name="price" type="float"/>
                  
</all>
              
</complexType>
           
</element>
       
</schema>
    
</types>

    
<message name="GetLastTradePriceInput">
        
<part name="body" element="xsd1:TradePriceRequest"/>
    
</message>

    
<message name="GetLastTradePriceOutput">
        
<part name="body" element="xsd1:TradePrice"/>
    
</message>

    
<portType name="StockQuotePortType">
        
<operation name="GetLastTradePrice">
           
<input message="tns:GetLastTradePriceInput"/>
           
<output message="tns:GetLastTradePriceOutput"/>
        
</operation>
    
</portType>

    
<binding name="StockQuoteSoapBinding" type="tns:StockQuotePortType">
        
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        
<operation name="GetLastTradePrice">
           
<soap:operation soapAction="http://example.com/GetLastTradePrice"/>
           
<input>
               
<soap:body use="literal"/>
           
</input>
           
<output>
               
<soap:body use="literal"/>
           
</output>
        
</operation>
    
</binding>

    
<service name="StockQuoteService">
        
<documentation>My first service</documentation>
        
<port name="StockQuotePort" binding="tns:StockQuoteBinding">
           
<soap:address location="http://example.com/stockquote"/>
        
</port>
    
</service>

</definitions>

SOAP1.1和1.2,基本都在使用,大多數(shù)都支持兩個(gè)版本。

<?xml version="1.0" encoding="UTF-8"?>
<
soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<
soapenv:Body>
   <
ns1:deletePictureService soapenv:encodingStyle="http://schemas.xmlsoap.org  /soap/encoding/" xmlns:ns1="deletePictureService">
  <
id xsi:type="xsd:string">121</id>
  </ns1:deletePictureService>
 </
soapenv:Body>
</
soapenv:Envelope>

2.Axis實(shí)現(xiàn)
Axis的介紹這里就不描述了,詳見(jiàn)其網(wǎng)站http://ws.apache.org/axis/
下載下來(lái)看其文檔基本就可以學(xué)習(xí)了,文檔相對(duì)比較詳細(xì)。

3.開(kāi)發(fā)和測(cè)試SOAP監(jiān)控

Axis的錯(cuò)誤提示不是很友好,通常會(huì)報(bào)莫名其妙的錯(cuò)誤,但是又不知道錯(cuò)誤的原因,這種情況下監(jiān)控Web Service的運(yùn)行情況非常有必要,可以通過(guò)下面的tcpmornitor.bat實(shí)現(xiàn),具體代碼如下:


1 set Axis_Lib=D:"workspace"TestOFBizServices"lib
2 
3 set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
4 
5 %Java_Cmd% org.apache.axis.utils.tcpmon


其中的Axis_libAxis自帶的lib庫(kù),里面有axis運(yùn)行需要的jar包。

運(yùn)行tcpmornitor.bat,顯示圖形界面工具:


輸入監(jiān)聽(tīng)的端口,目標(biāo)主機(jī)和端口,點(diǎn)擊“Add”按鈕即可。注意的是,目標(biāo)主機(jī)和端口是指實(shí)際響應(yīng)web service的主機(jī)和端口,而監(jiān)聽(tīng)端口是客戶端請(qǐng)求的端口。比如web service發(fā)布在192.168.22.73:8091上面,那么本地測(cè)試時(shí),請(qǐng)求的endpoint中主機(jī)和端口必須改成localhost:Listen Port。也就是說(shuō)TCPMornitor只是一個(gè)Proxy,它截獲并轉(zhuǎn)發(fā)SOAP請(qǐng)求和響應(yīng)的內(nèi)容,不做其他處理。增加了監(jiān)聽(tīng)后,出現(xiàn)監(jiān)聽(tīng)窗口:


此時(shí)啟動(dòng)客戶端的web service調(diào)用請(qǐng)求,成功處理后,監(jiān)控窗口會(huì)出現(xiàn)本次調(diào)用的監(jiān)控內(nèi)容:

上下兩個(gè)窗口分別顯示請(qǐng)求內(nèi)容和相應(yīng)內(nèi)容,通過(guò)查看SOAP消息可以輔助程序的調(diào)試。



Axis在Apache已不再維護(hù),推薦使用http://cxf.apache.org/