在《使用Eclipse的Axis1.4插件開(kāi)發(fā)Web Service及客戶端》介紹的Axis是用的Axis1.x,而Axis2是改進(jìn)版本,進(jìn)行了重新的設(shè)計(jì),應(yīng)該是Axis的第二代,本文介紹Axis2的使用方法。
http://ws.apache.org/axis2/是Axis2的主頁(yè),寫(xiě)這篇文章的時(shí)候最新版本是1.5,從http://ws.apache.org/axis2/download/1_5/download.cgi下載axis2-1.5-bin.zip、axis2-1.5-war.zip。
axis2-1.5-war.zip是web發(fā)布版本,可以在網(wǎng)頁(yè)上進(jìn)行Web Service的動(dòng)態(tài)部署,提供Web Service服務(wù)。解壓縮后把a(bǔ)xis2.war放到tomcat的webapps下面,啟動(dòng)tomcat,在網(wǎng)頁(yè)中輸入http://localhost:8080/axis2(取決于tomcat的設(shè)置)就可以進(jìn)行操作了。這方面的資料很多,就不詳細(xì)介紹了。
下面介紹使用Eclipse插件開(kāi)發(fā)的方法,環(huán)境與《使用Eclipse的Axis1.4插件開(kāi)發(fā)Web Service及客戶端》的相同。
啟動(dòng)Eclipse后,首先設(shè)置Axis2的主目錄,需要先解壓縮axis2-1.5-bin.zip,然后把解壓縮之后的路徑填入Eclipse的Window|Preferences|Web Services|Axis2 Preferences的界面上,如下圖所示:
圖1在Eclipse中設(shè)置Axis2的runtime
建立一個(gè)簡(jiǎn)單的類(lèi):
package demo.axis;
public class User
{
public String sayHello(String userName)
{
return "hello " + userName;
}
}
public class User
{
public String sayHello(String userName)
{
return "hello " + userName;
}
}
把這個(gè)類(lèi)發(fā)布為Web Service的步驟與《使用Eclipse的Axis1.4插件開(kāi)發(fā)Web Service及客戶端》中的相同,只是要注意選擇Axis2,生成之后的目錄結(jié)構(gòu)是:
圖2生成Web Service后的目錄結(jié)構(gòu)
但生成之后的項(xiàng)目中缺少.jar文件,可以把%AXIS2-HOME%/lib下的所有文件復(fù)制到項(xiàng)目的WEB-INF/lib下面,部署到tomcat就可以運(yùn)行了,在瀏覽器中輸入http://localhost:8080/Axis2ServerDemo/services/User?wsdl就可以看到發(fā)布的Web Service的wsdl了。
除了利用Eclipse插件生成Web Service之外,自己動(dòng)手部署也不困難。建立一個(gè)目錄,把需要部署為Web Service的class文件放到一個(gè)目錄下面,并建立一個(gè)META-INF目錄,把services.xml放入META-INF下。目錄結(jié)構(gòu)如下:
圖3Axis2 aar目錄結(jié)構(gòu)
用jar命令把以上內(nèi)容打包為一個(gè)aar文件,命令為“jar cvf User.aar *.*”。這樣就會(huì)生成一個(gè)User.aar文件,放到應(yīng)用的WEB-INF/services下面,也就部署了Web Service。
其中services.xml的內(nèi)容可以是:
<service name="User" >
<Description>
Please Type your service description here
</Description>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<parameter name="ServiceClass" locked="false">demo.axis2.User</parameter>
</service>
<Description>
Please Type your service description here
</Description>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<parameter name="ServiceClass" locked="false">demo.axis2.User</parameter>
</service>
這樣類(lèi)中所有public方法都發(fā)布為Web Service,如果只想發(fā)布類(lèi)的某些方法,services.xml可以這樣寫(xiě):
<service name="User" >
<Description>
Please Type your service description here
</Description>
<parameter name="ServiceClass" locked="false">demo.axis2.User</parameter>
<operation name="sayHello">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
<Description>
Please Type your service description here
</Description>
<parameter name="ServiceClass" locked="false">demo.axis2.User</parameter>
<operation name="sayHello">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
客戶端的編寫(xiě)可以直接寫(xiě)代碼,也可以用Axis2提供的命令wsdl2java生成客戶端的代碼。
直接寫(xiě)代碼的方式,示例代碼如下:
package demo.axis2.client;
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class UserClient
{
public static void main(String[] args) throws Exception
{
// 使用RPC方式調(diào)用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定調(diào)用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/Axis2ServerDemo/services/User");
options.setTo(targetEPR);
// 指定方法的參數(shù)值
Object[] opAddEntryArgs = new Object[] {"tom"};
// 指定方法返回值的數(shù)據(jù)類(lèi)型的Class對(duì)象
Class[] classes = new Class[] {String.class};
// 指定要調(diào)用的getGreeting方法及WSDL文件的命名空間
QName opAddEntry = new QName("http://axis2.demo", "sayHello");
// 調(diào)用方法并輸出該方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
}
}
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class UserClient
{
public static void main(String[] args) throws Exception
{
// 使用RPC方式調(diào)用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定調(diào)用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/Axis2ServerDemo/services/User");
options.setTo(targetEPR);
// 指定方法的參數(shù)值
Object[] opAddEntryArgs = new Object[] {"tom"};
// 指定方法返回值的數(shù)據(jù)類(lèi)型的Class對(duì)象
Class[] classes = new Class[] {String.class};
// 指定要調(diào)用的getGreeting方法及WSDL文件的命名空間
QName opAddEntry = new QName("http://axis2.demo", "sayHello");
// 調(diào)用方法并輸出該方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
}
}
用wsdl2java命令的方式,首先要生成代碼,命令如下:
set AXIS2_HOME=E:/Program/WebService/axis2-1.5
%AXIS2_HOME%/bin/wsdl2java -uri http://localhost:8080/Axis2ServerDemo/services/User?wsdl -p client -s -o stub
%AXIS2_HOME%/bin/wsdl2java -uri http://localhost:8080/Axis2ServerDemo/services/User?wsdl -p client -s -o stub
生成客戶端訪問(wèn)代碼后,調(diào)用方法如下:
package demo.axis2.client;
import client.UserStub;
public class Stub
{
public static void main(String[] args) throws Exception
{
UserStub stub = new UserStub();
UserStub.SayHello sayHello = new UserStub.SayHello();
sayHello.setUserName("tom");
System.out.println(stub.sayHello(sayHello).get_return());
}
}
import client.UserStub;
public class Stub
{
public static void main(String[] args) throws Exception
{
UserStub stub = new UserStub();
UserStub.SayHello sayHello = new UserStub.SayHello();
sayHello.setUserName("tom");
System.out.println(stub.sayHello(sayHello).get_return());
}
}