2011年8月1日 星期一

在 eclipse 中開發 web service

jdk 6.0
eclipse ganymede ee
tomcat 6.0
上述工具先裝好

一、
下載 Axis2
http://ws.apache.org/axis2/download/1_4_1/download.cgi
安裝 Axis2
Window>Preference>Web Services>Axis2 Preferences

二、
建立 Dynamic Web Project
建立一個 java application 如下
package com.tdl;

public class HelloWorld {

public String echo(){
return "Hellow Web Service.";
}
}

三、
在上述 HelloWorld 上按右鍵>Web Service>Create Web Service
Web Service Runtime 改成 Axis2
Next>Next>Start Server>Finish

四、
看一下 WebContent 下是不是多了一些檔案
WebContent>axis2-web>*.jsp

五、
在 index.jsp 上按右鍵>Run As>Run on Server>Finish
看到畫面的話就表示已成功配置 web service 了
再點選 Services>HelloWorld
應該會帶出如下的 url 和一份 wsdl 文件檔
http://localhost:8080/bonus/services/HelloWorld?wsdl

六、
再來要建立 Client 端
另外建一個 Dynamic Web Project 在專案名稱上按右鍵
File>New>Other>Web Services>Web Service Client
service definition 填入上述的
http://localhost:8080/bonus/services/HelloWorld?wsdl

Web Service Runtime 改成 Axis2
Next>Finish

七、
再建一個 java application 當作執行檔,範例如下:

package com.tdl;
import com.tdl.HelloWorldStub.Echo;
import com.tdl.HelloWorldStub.EchoResponse;

public class TestClient {

/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
HelloWorldStub stub = new HelloWorldStub();
HelloWorldStub.Echo stubEcho = new HelloWorldStub.Echo();
EchoResponse returnValue = stub.echo( stubEcho ) ;
System.out.println( "return value = "+ returnValue.get_return() );
}
}

完成後 Run As > Java Application

即可看到結果

實用的 AspectJ

最近用了 「AJDT」 和 「Spring + AspectJ」這兩種 AOP 的方式。

AJDT 用於 Eclipse 內開發,需要撰寫 .aj 檔。

Spring + AspectJ 限用於以 Spring 管理的 bean。
範例下載 https://github.com/luyingzhang/aspectj-tutorial/blob/master/README