JAX-WX2.0下的Web Service開發經驗

運行平臺

IBM Websphere Application Service 6.1 + Web Service Feature Pack

 

開發平臺

IBM RAD 7.0

 

在jax-ws2.0規範下,一切都得遵循它,在項目初始階段,必定會需要很多困難,下面幾條注意事項是本人在實際Jax-ws Web Service項目開發時的一些經驗總結,希望能對大家有所幫助

 

1. Web service的Exception問題。

       好的應用程序,對Exception的合理處理是必不可少的,Web Service作爲一種遠程的應用程序,會涉及到很多的錯誤處理,比如服務器發生致命應用程序錯誤及其它一些runtime錯誤等,還有一些應用程序本身傳遞的異常,如產品代碼不規範的錯誤異常等,所有的這些異常都需在經過Web Service接口層,傳遞給調用它的客戶端應用程序,進行相關的處理,但是目前的Web Service異常有其特殊的要求,這就使用普通的java exception不能被正確地被客戶端捕捉,在jax-ws2.0規範中,對Web service的exception要求爲

 

(JSR-224)

 2.5 Fault
A wsdl:fault element is mapped to a Java exception.

Conformance (javax.xml.ws.WebFault required): A mapped exception MUST be annotated with a
javax.xml.ws.WebFault annotation.


Conformance (Exception naming): In the absence of customizations, the name of a mapped exception
MUST be the value of the name attribute of the wsdl:message referred to by the wsdl:fault element
mapped according to the rules in sections 2.8 and 2.8.1.


An application MAY customize thismapping using the jaxws:class binding declaration defined in section
8.7.4.


Multiple operations within the same service can define equivalent faults. Faults defined within the same
service are equivalent if the values of their message attributes are equal.

 Conformance (Fault equivalence): An implementation MUST map equivalent faults within a service to a single Java exception class.


A wsdl:fault element refers to a wsdl:message that contains a single part. The global element declaration3
referred to by that part is mapped to a Java bean, henceforth called a fault bean, using the mapping
described in section 2.4. An implementation generates a wrapper exception class that extends java.lang-
.Exception and contains the following methods:


WrapperException(String message, FaultBean faultInfo) A constructor where WrapperException
is replaced with the name of the generated wrapper exception and FaultBean is replaced by the
name of the generated fault bean.
3WS-I Basic Profile[18] R2205 requires parts to refer to elements rather than types.
22 JAX-WS 2.1 May 7, 2007

WrapperException(String message, FaultBean faultInfo, Throwable cause) A constructor
whereWrapperException is replaced with the name of the generated wrapper exception and FaultBean
is replaced by the name of the generated fault bean. The last argument, cause, may be used to convey
protocol specific fault information, see section 6.4.1.


FaultBean getFaultInfo() Getter to obtain the fault information, where FaultBean is replaced by the
name of the generated fault bean.


The WrapperException class is annotated using the WebFault annotation (see section 7.2) to capture the
local and namespace name of the global element mapped to the fault bean.


Two wsdl:fault child elements of the same wsdl:operation that indirectly refer to the same global
element declaration are considered to be equivalent since there is no interoperable way of differentiating
between their serialized forms.


Conformance (Fault equivalence): At runtime an implementation MAY map a serialized fault into any
equivalent Java exception.

 

總結上文,就是得國@WebFault Anotation, 要兩個構造方法,還要有一個getFaultInfo()的方法,構造方法有一個FaultBean的參數,getFaultInfo()返回這個FaultBean,這個Fault實際爲一個實現了Serializable接口的標準javaBean.

 

如果你不照着上面的步驟做,那麼你在客戶端調用時,一旦服務器出錯,拋出異常,在客戶端全部都會成爲runtime異常,程序掛掉。

 

2. 同一類中不能出現相同的方法名,即方法是不能重載的

 

解釋同3

 

3. 同一package下的任意兩個類都不能有相同的方法名

 

原因:在RAD開發Web Service時(注意:在創建項目須勾選“Web Service Feature Pack..”),在有Web Service endpoint類的package下,會生成一個jaxws的文件夾,它裏面會根據JAXB生成一系列的類,沒錯,它是一個package共用的,所以,當package下任意有兩個類有相同的方法名時,其中一個就會被另一個生成的類替換掉了,在實際調用時被覆蓋掉的方法時,就會出現UnmarshalException

 

4. 方法的返回值不能爲List類型

 

應該是出於語言無關的要求,List類型是不被支持的,只支持Object Array類型,即數組或普通的類型,否則,這個方法是無效的,客戶端不能對其進行調用

 

5. 方法參數中有Object Array類型時,必須在方法裏判斷它是否爲null

 

 原因:在用Wsimport工具生相關wsdl文件的客戶端代碼時,會將Object Array的參數影射爲帶有Generic的List類型,如果客戶在調用這些帶Object Array的方法時,傳入一個size=0的list, 經過xml影射,到達服務器端時,Object Array會成爲null值,而非length=0的Object Array

 

6. 提供調用webservice的驗證信息

 

有些web service需要調用者提供身份驗證信息,最基本的就是base authentication,也就是平常打開一個wsdl時,瀏覽器會彈出一窗口要求提供用戶名與密碼的那種。

 

在java中要調用這種帶驗證信息的web service,首先需要先得到wsdl文件,並保存在某一文件夾中,再用java自帶的wsimport.exe對wsdl生成相對應的java調用文件,調用wsimport時,還需提供一wsdllocation參數,用於指定真實的wsdl文件的位置(不然默認爲本地的wsdl地址),然後在調用webserivce前,還需在調用代碼前用Authenticator類提供驗證信息,這樣代碼才能訪問網絡上需驗證的wsdl文件。

 

如果是調用.net生成的web service,有時還需要用到BindingProvider,將service port強轉爲BindingProvider,再通過BindingProvider提供的方法添加調用web service要用到的head信息,在這裏我們提供的是驗證的頭信息.

 

7. webservice的方法實現傳址調用

 

顧名思義,就是在調用web service的方法時傳一object過去,再將結果直接設到傳入的object中,無返回結果。要實現這種效果,在jax-ws2.0中很簡單,只需將@WebParam與@WebResult的name設爲同一值即可。

 

 

8. 實現webservice的身份驗證功能

 

其一就是用Base Authentication,即彈出一窗口讓用戶輸入身份驗證信息,但這種身份驗證僅限於加密wsdl文件,我們需同時要對對應的web service程序進行加密。至於如何在web.xml中使用Base驗證,在此不與詳述。

 

 

 

發佈了38 篇原創文章 · 獲贊 1 · 訪問量 5871
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章