網(wǎng)上關(guān)于axis2做webservice的中文文章并不多
axis2確實(shí)和axis1有很大不同
其中返回自定義對象的方法似乎就不兼容(可能僅僅是我還沒研究出來), axis1返回對象或?qū)ο罅斜砜梢灾苯釉诜椒ǚ祷仡愋椭袑懨? serverlet會自動翻譯成wsdl, 雖然axis2也可以自動翻譯, 但是用過的客戶端生成工具都沒法直接使用此服務(wù), 最后都?xì)w于"到OMElemet 對象無法轉(zhuǎn)換", 所以我的方法只好自己寫轉(zhuǎn)換函數(shù), 我想這個應(yīng)該不是axis2的標(biāo)準(zhǔn)方法吧, 怎么會越來越麻煩?
下面講一下我的使用流程
需求: 做個webservice服務(wù), 從數(shù)據(jù)庫里面讀取flightleg的一個列表并返回, flightleg列表可以認(rèn)為是經(jīng)過一個select語句返回的記錄集合
軟件環(huán)境, windows xp sp2, tomcat5.5 for windows 單一安裝版, jre1.5(tomcat5.5的需要), axis2-0.95(當(dāng)時是最新版)
試驗(yàn)流程:
???????直接安裝 tomcat5.5 for windows , 發(fā)現(xiàn)服務(wù)總是在啟動之后馬上自己關(guān)閉, 查找原因, 發(fā)現(xiàn)原來自己用的都是jre1.4, 后從別地拷貝一個jre_1.5目錄到Program Files\j2sdk1.4.2_05中, 然后設(shè)置tomcat的 java virtual machine 為Program Files\j2sdk1.4.2_05\jre_1.5\bin\client\jvm.dll , 即可正常啟動.
?????? axis2安裝, 這個網(wǎng)上介紹的很多, 也很容易, 就下載那個axis2.war包, 拷貝到Program Files\Apache Software Foundation\Tomcat 5.5\webapps\目錄中, 就算發(fā)布了, tomcat自動感知, 并生成axis2這個目錄結(jié)構(gòu).? 進(jìn)入http://localhost:8080/axis2/?可以看到歡迎頁面, 然后點(diǎn)擊Services 可以看到幾個示范serviece, 具體怎么使用就不多說了, 網(wǎng)上不少, 自帶的doc也有說明.
????? 編寫service程序, 后面邏輯就不管了, 關(guān)鍵要編寫個serviceproxy類,? 并在里面暴露方法作為webserivce調(diào)用, sample里面也有很多, 但是至今還沒看到返回自定義對象的. 我寫的如下:
????
public
?OMElement?getPVGFlightLegs()

????
{
????????List?list?
=
?
null
;
????????InquiryEngine?engine?
=
?InquiryFactory.getInquiryEngine();
????????list?
=
?(List)engine.getPVGFlightLegs();
????????OMElement?ele?
=
?
null
;
????????ele?
=
?createOMElementFromList(list);
????????
return
?ele;
????}
???? 返回的是OMElement 統(tǒng)一對象, 需要自己構(gòu)造OMElement對象樹并返回,? createOMElementFromList() 代碼如下:
????
??? public OMElement createOMElementFromList(List flightLegInfoList)??
??? {
???????
??????? OMFactory fac = OMAbstractFactory.getOMFactory();
??????? OMNamespace omNs = fac.createOMNamespace("http://localhost:8080/axis2/services/FidsService", "fids");
??????? OMElement resp = fac.createOMElement("flightLeglist", omNs);
??????? for(Iterator it = flightLegInfoList.iterator(); it.hasNext(); )
??????? {
??????????? FlightLegInfo flightLegInfo = (FlightLegInfo)it.next();
???????? OMElement record = fac.createOMElement("record", omNs);
???????? OMElement flightNoElement = fac.createOMElement("flightNo", omNs);
???????? OMElement tailNoElement = fac.createOMElement("tailNo", omNs);
???????? flightNoElement.addChild(fac.createText(flightNoElement, flightLegInfo.getFlightNo()));
???????? tailNoElement.addChild(fac.createText(tailNoElement, flightLegInfo.getTailNo()));
???????? record.addChild(flightNoElement);
???????? record.addChild(tailNoElement);
???????? resp.addChild(record);
??????? }
??????? return resp;
??? }
???? 代碼的含義是返回包含flightNo和tailNo的record列表, 構(gòu)造成XML樹.
???? web-inf\services.xml 如下:
<
service?
name
="FidsService"
>
????
<
description
>
????????This?is?a?fids?web?service.
????
</
description
>
????
<
parameter?
name
="ServiceClass"
?locked
="false"
>
com.cea2.service.fids.ServiceProxy
</
parameter
>
????
<
operation?
name
="getPVGFlightLegs"
>
????????
<
messageReceiver?
class
="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"
/>
????
</
operation
>
</
service
>
?????? 然后把所有的class,lib,web-inf\services.xml 等組織起來,打個jar包, 并改后綴為aar (這個過程有個eclipse插件Axis2_Service_Archiver可以完成這個過程, 但不太會用, 還事先需要.wsdl文件??), 并拷貝到Program Files\Apache Software Foundation\Tomcat 5.5\webapps\axis2\WEB-INF\services\中. 然后重啟tomcat,? 進(jìn)入http://localhost:8080/axis2/?看services, 里面有FidsService, 點(diǎn)擊可以看到wsdl, 服務(wù)端算成功大半了.
????? 客戶端: 在做返回自定義對象的時候, 用了幾個eclipse自動生成工具, 甚至還用了axis2自帶命令行wsdl2java, 都不行. 改為返回OMElement的時候, 就干脆沒有用gererator,? 直接寫了testClient類, 并把service端中的FlightLegInfo(對應(yīng)單條記錄對象)拷貝過來.? testClient如下
package
?test;

import
?org.apache.axis2.AxisFault;
import
?org.apache.axis2.Constants;
import
?org.apache.axis2.addressing.EndpointReference;
import
?org.apache.axis2.client.Options;
import
?org.apache.axis2.client.ServiceClient;
import
?org.apache.ws.commons.om.OMElement;
import
?org.apache.ws.commons.om.OMNode;

import
?javax.xml.stream.XMLOutputFactory;
import
?javax.xml.stream.XMLStreamException;
import
?java.io.StringWriter;
import
?java.util.ArrayList;
import
?java.util.Iterator;
import
?java.util.List;


/**?*/
/**
?*?Sample?for?synchronous?single?channel?blocking?service?invocation.
?*?Message?Exchage?Pattern?IN-OUT
?
*/
public
?
class
?testClient?
{
????
private
?
static
?EndpointReference?targetEPR?
=
?
new
?EndpointReference(
"
http://localhost:8080/axis2/services/FidsService
"
);


????
public
?
static
?
void
?main(String?args[])?
{

????????
try
?
{
??????????
????????????
????????????OMElement?payload?
=
?ClientUtil.getEchoOMElement();
????????????Options?options?
=
?
new
?Options();
????????????options.setTo(targetEPR);
????????????options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
????????????options.setAction(
"
getPVGFlightLegs
"
);

????????????
//
Blocking?invocation
????????????ServiceClient?sender?
=
?
new
?ServiceClient();
????????????sender.setOptions(options);
????????????OMElement?result?
=
?sender.sendReceive(payload);
?
????????????
//
顯示xml
????????????StringWriter?writer?
=
?
new
?StringWriter();
????????????result.serialize(XMLOutputFactory.newInstance()
????????????????????.createXMLStreamWriter(writer));
????????????writer.flush();
??????????
????????????System.out.println(writer.toString());
????????????
//
System.out.println("first:");
????????????
//
結(jié)果轉(zhuǎn)換為list
????????????List?list?
=
?getResults(result);

????????}
?
catch
?(AxisFault?axisFault)?
{
????????????axisFault.printStackTrace();

????????}
??
catch
?(XMLStreamException?e)?
{
????????????e.printStackTrace();
????????}
????}
????

????
private
?
static
?List?getResults(OMElement?element)?
{
????????Iterator?iterator?
=
?element.getChildElements();
????????List?list?
=
?
new
?ArrayList();

????????
while
?(iterator.hasNext())?
{
????????????OMNode?omNode?
=
?(OMNode)?iterator.next();

????????????
if
?(omNode.getType()?
==
?OMNode.ELEMENT_NODE)?
{
????????????????OMElement?omElement?
=
?(OMElement)?omNode;

????????????????
if
?(?omElement.getLocalName().equals(
"
record
"
)?)?
{
????????????????????FlightLegInfo?flightLegInfo?
=
?getFlightLegInfo(omElement);
????????????????????list.add(flightLegInfo);
????????????????}
????????????}
????????}
????????
return
?
null
;
????}
????
????
private
?
static
?FlightLegInfo?getFlightLegInfo(OMElement?element)

????
{
????????FlightLegInfo?flightLegInfo?
=
?
new
?FlightLegInfo();
????????Iterator?iterator?
=
?element.getChildElements();

????????
while
?(iterator.hasNext())?
{
????????????OMNode?omNode?
=
?(OMNode)?iterator.next();

????????????
if
?(omNode.getType()?
==
?OMNode.ELEMENT_NODE)?
{
????????????????OMElement?omElement?
=
?(OMElement)?omNode;

????????????????
if
?(?omElement.getLocalName().equals(
"
flightNo
"
)?)?
{
????????????????????flightLegInfo.setFlightNo(omElement.getText());
????????????????????System.out.println(
"
flightNo:
"
+
omElement.getText());
????????????????}
????????????????
if
?(?omElement.getLocalName().equals(
"
tailNo
"
)?)?
{
????????????????????flightLegInfo.setTailNo(omElement.getText());
????????????????}
????????????}
????????}
????????
return
?flightLegInfo;
????}
}
?
???? 需要自己轉(zhuǎn)換成FlightLegInfo?的list, 圖方便, 直接在轉(zhuǎn)換函數(shù)里面打印了一些測試輸出數(shù)據(jù).? 算是完成了service的開發(fā).