一、工具
1、下載 Axis2以及eclipse的Axis2插件。http://axis.apache.org/axis2/java/core/download.cgi

2、axis2-1.7.1-war.zip解壓,將壓縮包內的axis2.war部署到%TOMCAT-HOME%/webapps下,啟動tomcat,訪問http://localhost:8080/axis2/看是否正常。
點擊Service會進入Service列表頁面,當前只有一個Version服務。http://localhost:8080/axis2/services/Version?wsdl
3、解壓縮eclipse插件 axis2-eclipse-codegen-plugin-1.7.1.zip,axis2-eclipse-service-plugin-1.7.1.zip。解壓后將plugins 復制到%ECLIPSE_HOME%\plugins。

二、Axis2發布Webservice
a、新建名稱為Axis2Service1 的java工程。新建TestWs.java

b.1、打包部署--arr部署方式
有手動打包和插件打包兩種方式,在此我用了插件打包的方式
- 手動打包
- 新建\Axis2Service1\deploy文件夾,將\Axis2Service1\bin下的class文件復制過來。
- 新建\Axis2Service1\deploy\META-INF\services.xml文件
<service name="AxisService"> <description>AxisService</description> <parameter name="ServiceClass">ws.TestWs</parameter> <operation name="showName"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> <operation name="getName"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> </operation> </service>
生成aar包 \Axis2Service1\deploy>jar cvf AxisService.aar . (注意帶.號)
2 . 插件打包
- IDE中選擇New->other->Axis2 Service Archiver,點擊Next;
- Class File Location:選擇Axis2Service1\bin目錄,點擊Next;
- 勾選Skip WSDL,點擊Next;
- Service Archiver 選擇jar位置,如果沒有jar包就直接點擊Next;
- 勾選Generate the service xml automatically 自動生成service.xml file文件,點擊Next
- service name,輸入:AxisService,然后在class name 中填寫要發布的類(全路徑,如:ws.TestWs),點擊load。勾選 Search declared methods only。點擊next
- output File location,輸入:D:\ ; output File Name,輸入artiver文件的名稱 AxisService。點擊finish。
- 提示 Service Archvie generated successfully! 注冊表明,生成成功。
3、發布AxisService
AxisService.aar復制到%TOMCAT-HOME%/webapps/axis2/WEB-INF/services下。(不打aar包,\Axis2Service1\deploy下面復制過去也是可以)
打開http://localhost:8080/axis2/services/listServices 就可以看到剛才發布的AxisService服務了,下面有兩個函數:showName,getName。
b.2 獨立部署
1、新建java web project工程。
2、文件復制
%TOMCAT-HOME%\webapps\axis2\WEB-INF\lib 復制到 \Axis2Service2\WebRoot\WEB-INF\lib 下,並加入工程引用。
%TOMCAT-HOME%\webapps\axis2\WEB-INF\conf 復制到 \Axis2Service2\WebRoot\WEB-INF\conf
%TOMCAT-HOME%\webapps\axis2\WEB-INF\modules 復制到 \Axis2Service2\WebRoot\WEB-INF\modules
3、web.xml 代碼如下
<?xml version="1.0" encoding="UTF-8"?> <web-app id="wmf" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> </web-app>
2、新建 \Axis2Service2\src\ws\TestWs.java
package ws; public class TestWs { public String showName(String name) {return name; } public String getName() {return "Axis2Service Sample"; } }
3、新建\Axis2Service2\WebRoot\WEB-INF\services目錄。
4、新建一個AxisService服務
AxisService\META-INF\services.xml
<service name="AxisService"> <description>AxisService</description> <parameter name="ServiceClass">ws.TestWs</parameter> <operation name="showName"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> <operation name="getName"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> </operation> </service>
啟動tomcat后,訪問http://localhost:8085/Axis2Service2/services/AxisService?wsdl看是否正常。
三、AXIS2調用Web Services
一、客戶端stub文件生成
我用的是插件生成方式
1、腳本生成方式
去AXIS2的解壓目錄下bin(%AXIS2_HOME%\bin\)下執行下面語句
wsdl2java -uri http://localhost:8085/Axis2Service2/services/AxisService?wsdl -p ws -s -o stub
-p參數指定了生成的Java類的包名
-o參數指定了生成的一系列文件保存的根目錄
在stub\src\ws自動生成AxisServiceStub.java
2、插件生成方式
IDE中選擇New->other->Axis2 Code Generator,點擊Next;
勾選Generate Java source code from a WSDL file,點擊Next;
WSDL file location,輸入:http://localhost:8085/Axis2Service2/services/AxisService?wsdl,點擊Next;
如果路徑不對會提示:Specified WSDL is invalid!, Please select a validated *.wsdl/*.xml file on previous page. 正確的話,點擊next;
指定輸入路徑,點擊Next 提示:All operations completed successfully! 生成成功。
在D:\src\ws 自動生成了stub一系列文件,其中ws是包名。

上面2種方式生成的stub類有點不一樣,腳本生成方式是單一文件,插件生成方式生成的一系列文件。
二、客戶端調用 腳本生成方式為例子,插件生成的類似。
1、新建 java工程 Axis2Client
新建\Axis2Client\lib文件夾 將%AXIS2_HOME%\lib\ 下的所有jar包復制到\Axis2Client\lib,並加入工程引用中,加入工程引用的方式如下
點擊工程右鍵--》Build Path --->Configure Build Paht--->

將通過插件生成在stub文件加入到src\ws下,(若是腳本生成方式,則是單一AxisServiceStub.java文件加入到src\ws下 )

2、新建TestWs.java,在test包下 主要代碼如下
//初始化Sub類 AxisServiceStub stub = new AxisServiceStub(); //傳遞AxisServiceStub.ShowName對象,相關參數在這邊賦值。 AxisServiceStub.ShowName command = new AxisServiceStub.ShowName(); command.setName("Hello!"); //取得返回值 String name = stub.showName(command).get_return(); System.out.println(name);
調用成功后控制台輸出:Hello!
擴展學習---AXIS2調用REST Web Services
使用http://localhost:8086/Axis2Rest/services/AxisService/showName?name=rest的方式訪問剛才發布成功的WebService。從上面可以看出這個就是rest風格。
Axis1.0是無法通過showName?name=rest來獲取信息的。
2、使用axis客戶端調用
public class TestRest { private static String toEpr = "http://localhost:8086/Axis2Rest/services/AxisService"; public static void main(String[] args) throws AxisFault { Options options = new Options(); options.setTo(new EndpointReference(toEpr)); //客戶端REST方式調用服務跟普通服務的區別,REST調用必須加上下面這個代碼。 options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE); ServiceClient sender = new ServiceClient(); //axis2-1.5.4不需要下面這句代碼,否則會報錯 //sender.engageModule(new QName(Constants.MODULE_ADDRESSING)); sender.setOptions(options); OMElement result = sender.sendReceive(getPayload()); try { XMLStreamWriter writer = XMLOutputFactory.newInstance() .createXMLStreamWriter(System.out); result.serialize(writer); writer.flush(); } catch (XMLStreamException e) { e.printStackTrace(); } catch (FactoryConfigurationError e) { e.printStackTrace(); } } private static OMElement getPayload() { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace( "http://ws", "example1"); OMElement method = fac.createOMElement("showName", omNs); OMElement value = fac.createOMElement("name", omNs); value.addChild(fac.createOMText(value, "Rest")); method.addChild(value); return method; }
說明:
1、sender.engageModule(new QName(Constants.MODULE_ADDRESSING)); axis2-1.5.4不需要下面這句代碼,否則會報錯
2、客戶端REST方式調用服務跟普通服務的區別,就是Rest有下面這個代碼;
options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);
兩者返回的數據都是
<ns:showNameResponse xmlns:ns="Resthttp://ws"><ns:return>Rest</ns:return></ns:showNameResponse>
3、getPayload方法
OMNamespace omNs = fac.createOMNamespace("http://ws", "example1");
指定命名空間,如果沒對的話會報如下錯誤namespace mismatch require http://ws found http://ws1 OMElement method = fac.createOMElement("showName", omNs);
要傳遞的方法名為 "showName" OMElement value = fac.createOMElement("name", omNs);
傳遞的參數為name value.addChild(fac.createOMText(value, "Rest"));
傳遞參數name的值為Rest。
