這個部份介紹該如何建立和佈署一個簡單的web service及client.
這個service的原始碼在tut-install/examples/jaxws/helloservice/以及client原始碼在
tut-install/examples/jaxws/simpleclient/
開發一個JAX-WS web service的最開始的出發點是一個javax.jws.WebService的Java class annotation.
@WebService annotation定義一個class為一個web service的終端點。
一個服務終端點介面(service endpoint interface)或是服務終端點承做(service endpoint implementation, SEI)
是一個Java介面或是類別,各自地宣告那些client可以在service上呼叫的方法,當建立一個JAX-WS終端點,介面是不一定需要的。
web service暗示地承做類別被定義為SEI
你也可以定義一個清楚的介面藉由加上endpointInterface元素到@WebService annotation在一個承做類別裡。
你必須提供一個介面定義public方法使得端點承做類別是有效的。
你使用endpoint implementation class和wsgen工具來產生連結一個web service client到JAX-WS runtime的加工品。
更多的wsgen請參考Sun GlassFish Enterprise Server v3 Reference Manual.
http://docs.sun.com/doc/820-7701
wsgen tool和Enterprise Server提供Enterpise Server來承做JAX-WS
下面是幾個製作web service跟client的基本步驟:
- 編寫implementation class.
- 編譯implementation class.
- 使用wsgen來產生佈署所需要的成品
- 打包檔案到一個WAR檔案裡
- 佈署WAR檔案,web service artifacts會在佈署時由Enterprise Server產生。
- 編寫client class
- 使用wsimport來產生和編譯需要連結到service用的web service artifacts
- 編譯client class
- 執行class
JAX-WS Endpoint的需求。
JAX-WS端點必須有下列的需求。
- 承做class一定要被annotated使用javax.jws.WebService或是javax.jws.WebServiceProvider。
- 承做class也許清楚地參考一個SEI透過@WebService annotation的endpointInterface元素,但是並不一
定要,如果沒有endpointInterface被定義在@WebService, 一個SEI是暗示地被定義到implementing class - 承做類裡的business方法一定要是public,也不能被宣告為static或是final
- Business方法被暴露給client的一定要annotated使用javax.jws.WebMethod
- Busines methods被暴露給web service client一定要有JAXB-compatible參數和回傳型態,參見
JAXB default data type bindings(http://java.sun.com/javaee/5/docs/tutorial/doc/bnazq.html#bnazs) - 承做類一定不能被宣告為final以及abstract
- 承做類一定要有一個預設的public建構子
- 承做類一定不能定義finalize方法
- 承做類在生命週期事件回覆一定使用javax.annotation.PostConstruct或是javax.annotation.PreDestroy annotations在它的方法上
在endpoint在運作上被卸除前,@PreDestroy方法被container呼叫。
No comments:
Post a Comment