1.首先在myeclipse裝axis2外掛程式,比較麻煩,各種狀況。
參照了:http://blog.csdn.net/seven_zhao/article/details/6089747
2.axis2.jar包下載,地址:http://axis.apache.org/axis2/java/core/download.cgi ,我用的版本是1.5.6
3.產生伺服器端工程yourProjectName
參照了:http://www.lifeba.org/arch/java_axis2_webservice.html 使用第三部分:獨立部署中的方法。
主要是複製WEB-INFO下的3個axis2相關檔案夾和web.xml,然後修改services.xml裡邊的服務名稱yourServiceName和方法名稱yourMethod。
如:服務類TestWs:
package ws;public class TestWs {public String showName(String name) {return name;}public String getName() {return "Hello,Axis2,,by getName method.";}}
services.xml配置如下:
<service name="AxisService"><description>AxisService</description><parameter name="ServiceClass">ws.TestWs</parameter><operation name="showName"><messageReceiverclass="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /></operation><operation name="getName"><messageReceiverclass="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /></operation></service>
工程名稱為Axis2Service
部署在tomcat下驗證服務是否正常:
http://localhost:81/yourProjectName/services/yourServiceName?wsdl
http://localhost:81/Axis2Service/services/AxisService?wsdl
4.產生用戶端測試程式
使用裝好的axis2外掛程式“Axis2 Code Generator” 產生代碼到指定工程下。
測試類別:
package test;import ws.AxisServiceStub;public class ClientTest {public static void main(String[] args) throws Exception {AxisServiceStub stub = new AxisServiceStub();AxisServiceStub.ShowName command = new AxisServiceStub.ShowName();command.setName("Hello,axis2,,by showName method.");String showNameRtn = stub.showName(command).get_return();System.out.println(showNameRtn);String getNameRtn = stub.getName().get_return();System.out.println(getNameRtn);}}
輸出:
Hello,axis2,,by showName method.
Hello,Axis2,,by getName method.