JAX-WX2.0下的Web Service开发经验

客户端调用方法时,总是返回null,集合的话所有元素都是空。但服务器端确是有值的,这个较郁闷。出现这个问题时搞了我好久。
解决方法:最好在implement类中用数组,不要用List.相关的其它问题可以参考‍http://gavin-chen.iteye.com/blog/347704,这位仁兄已经整理出来了。

运行平台

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验证,在此不与详述。

 

 

运行平台

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验证,在此不与详述。

 

 

posted @ 2016-06-06 17:20  七夏之  阅读(540)  评论(0编辑  收藏  举报