天天看點

使用Axis2建構Hello應用

A.    環境準備

a.     Eclipse 3.2.2

b.     MyEclipse 5.5m2

c.     Axis 2.1.1 War包 http://ftp.wayne.edu/apache/ws/axis2/1_1_1/axis2.war

d.     Axis Eclipse plug-in(代碼生成工具和打包工具):

http://apache.justdn.org/ws/axis2/tools/1_1_1/axis2-eclipse-codegen-wizard.zip

http://apache.justdn.org/ws/axis2/tools/1_1_1/axis2-eclipse-service-archiver-wizard.zip

B.    開發步驟

a.       建立一個web項目,名稱為hello

b.       生成wsdl檔案

打開MyEclipse的web service中的wsdl向導

檔案名為hello.wdsl,下一步

目标位址為http://www.oksonic.cn/hello/,其它為預設,完成

修改hello.wsdl檔案為以下格式

C.       根據wsdl檔案生成用戶端java代碼

使用Ctrl + N 打開Axis Wizards中的Axis2 Code Generator

使用預設的第一項,下一步

選擇生成的hello.wsdl檔案D:/oksonic/workspace/hello/src/hello.wsdl,下一步

參照下圖進行配置

解壓Axis 2.1.1 檔案到C槽,然後參照下圖進行配置

将加入進來的lib目錄移到WEB-INF目錄下,重新整理一下項目就可以看到生成的代碼了。

D.       根據wsdl檔案生成服務端java代碼

使用Ctrl + N 打開Axis Wizards中的Axis2 Code Generator

使用預設的第一項,下一步

選擇生成的hello.wsdl檔案D:/oksonic/workspace/hello/src/hello.wsdl,下一步

參照下圖進行配置

下一步

E.       修改HelloSkeleton代碼,内容如下:

package org.example.www.hello;

public class HelloSkeleton {

    public org.example.www.hello.HelloResponse hello(

           org.example.www.hello.Hello param0)    {

       try {

           HelloResponse response = new HelloResponse();

           response.setOut(param0.getIn() + "歡迎你的到來!");

           return response;

       } catch (UnsupportedOperationException e) {

           throw e;

       }

    }

}

F.       釋出項目

1.     打開Axis2的向導,選擇Axis2 Service Archiver

2.     選擇class檔案所在的目錄,如:D:/oksonic/workspace/hello/WebRoot/WEB-INF/classes

3.     選擇項目中resources目錄下的hello.wsdl檔案如:D:/oksonic/workspace/hello/resources/hello.wsdl

4.     跳過一下步,這裡選擇resources目錄下的service.xml檔案如:D:/oksonic/workspace/hello/resources/services.xml

5.     這一步是輸出一個jar檔案或是aar都可以,上面是輸出路徑,下面是檔案名,如:C:/Documents and Settings/Administrator/桌面,Hello.jar

6.     将axis 2.1.1 .war檔案拷貝到tomcat安裝路徑下的webapps目錄下,這裡可以選擇啟動tomcat來解開axis2.1.1.war,也可以直接将檔案改名為zip後解壓,解壓後的檔案夾名稱為axis2,然後将Hello.jar拷貝到asix2.1.1/WEB-INF/service目錄下

G.       測試

啟動tomcat服務,打開IE在位址欄中輸入http://localhost/axis2,進入頁面後點選Services連結,在下一個頁面中将會看到hello服務的連結,點開此連結就可以看到服務資訊了。

接下來使用用戶端測試代碼進行測試,修改HelloTest.java檔案中的testHello方法,内容如下:

public void testHello() throws java.lang.Exception {

       String url = "http://localhost/axis2/services/Hello";

       HelloStub stub = new HelloStub(url);

       Hello hello = (Hello) getTestObject(Hello.class);

       hello.setIn("oksonic");

       System.out.println(stub.Hello(hello).getOut());

       assertNotNull(stub.Hello(hello));

    }

這裡使用到了Junit,是以還需要将Junit的jar包加到項目中

為了制作更多更好的錄像教程,希望大家多多提供相關資料!!!

繼續閱讀