07年底的時候就開始學習web service,那時候學習Axis1.4并用于項目。這次公司又使用Axis,因為是遺留系統,本來使用的就是Axis。于是對Web Service和Axis1.4進行了一個學習總結。

1、WebService的概念
Web Service是一個比較新的分布式服務組件,本質上就是以標準化的方式實現企業內部各個不同服務系統之間的互調或者集成。實現以上目的需要三個元素:
1、服務B以一種標準化的語言告訴服務A它能提供什么服務,以及如何調用它,它的服務在哪里,這就是Web Service的服務描述,是what,how和where部分;
2、服務A要以一種標注化的通信消息格式告訴服務B,它想調用什么服務,以及相應的輸入參數。當服務完成后,B會以同樣標注化的方式返回相應的服務結果,這就是Web Service的服務消息的request和response部分;
3、服務B注冊到相應的地址,以便外部找到,是URL部分。

上面三個元素實際上對應于Web Services的三個組成部分,WSDLSOAP和UDDI。

WSDL1.1和2.0,其中1.1應用最為廣泛,支持最多,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,基本都在使用,大多數都支持兩個版本。

<?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實現
Axis的介紹這里就不描述了,詳見其網站http://ws.apache.org/axis/
下載下來看其文檔基本就可以學習了,文檔相對比較詳細。

3.開發和測試SOAP監控

Axis的錯誤提示不是很友好,通常會報莫名其妙的錯誤,但是又不知道錯誤的原因,這種情況下監控Web Service的運行情況非常有必要,可以通過下面的tcpmornitor.bat實現,具體代碼如下:


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庫,里面有axis運行需要的jar包。

運行tcpmornitor.bat,顯示圖形界面工具:


輸入監聽的端口,目標主機和端口,點擊“Add”按鈕即可。注意的是,目標主機和端口是指實際響應web service的主機和端口,而監聽端口是客戶端請求的端口。比如web service發布在192.168.22.73:8091上面,那么本地測試時,請求的endpoint中主機和端口必須改成localhost:Listen Port。也就是說TCPMornitor只是一個Proxy,它截獲并轉發SOAP請求和響應的內容,不做其他處理。增加了監聽后,出現監聽窗口:


此時啟動客戶端的web service調用請求,成功處理后,監控窗口會出現本次調用的監控內容:

上下兩個窗口分別顯示請求內容和相應內容,通過查看SOAP消息可以輔助程序的調試。



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