运行平台
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验证,在此不与详述。
分享到:
相关推荐
**正文** JAX-WS(Java API for XML Web Services...总之,JAX-WS 2.0 API是Java平台开发SOAP Web服务的重要工具,通过其提供的丰富特性和简洁的编程模型,简化了Web服务的开发和使用,促进了分布式系统的构建和协作。
### 设计与开发 JAX-WS 2.0 Web 服务 #### JAX-WS 技术概述 JAX-WS(Java API for XML Web Services)是Java平台中用于简化Web服务开发的一项关键技术。该技术提供了全面的支持,使得开发者能够在Java环境中轻松...
[奥莱理] RESTful Java 开发 (Jax-RS 2.0 实现) (英文版) [奥莱理] RESTful Java with JAX-RS 2.0 (E-Book) ☆ 图书概要:☆ Learn how to design and develop distributed web services in Java, using RESTful ...
JAX-WS(Java API for XML Web Services)2.0 是一种用于创建和访问 Web 服务的标准 Java API,它允许开发人员使用 Java 编写 Web 服务,并以 XML 的形式进行数据交换。JAX-WS 2.0 由 Java 社区进程 (JCP) 通过 JSR-...
JAX-WS 2.0 是Java API for XML Web Services的第二个主要版本,它引入了异步Web服务调用的功能,以解决在Service-Oriented Architecture(SOA)环境中开发响应迅速的Web服务客户端应用程序的挑战。由于Web服务调用...
JAX-RS(Java API for RESTful Web Services)是一个Java标准,它简化并促进了RESTful Web服务的开发。JAX-RS 2.0作为该标准的更新版本,引入了新的注解和方法来支持RESTful服务的构建。从定义资源到处理HTTP请求,...
**Java API for XML Web Services(JAX-WS)2.0**是Java平台上的一个标准,用于构建和消费Web服务。它提供了简化Web服务开发的API,使得开发者能够以更面向对象的方式处理Web服务。本篇文章将深入探讨JAX-WS 2.0的...
《RESTful Java with JAX-RS 2.0, 第二版》是一本专注于Java开发者如何通过JAX-RS(Java API for RESTful Web Services)2.0版本构建RESTful(Representational State Transfer)服务的书籍。REST是一种基于网络的...
RESTful Java with JAX-RS 2.0 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...
《使用JAX-RS 2.0、WebSockets和JSON开发RESTful服务》是一本针对Java EE 7 API的实用指南,由Bhakti Mehta和Masoud Kalali共同撰写。本书详细介绍了在Java平台上构建RESTful Web服务的完整流程,特别是在HTML5和...
JAX-RS(Java API for RESTful Web Services)是Java平台上用于开发RESTful服务的一个标准API。第二版中特别提到了JAX-RS 2.0版本的更新和改进,这包括但不限于: - **非阻塞I/O支持**:新增了对非阻塞I/O的支持,...
Developing RESTful Services with JAX-RS 2.0, WebSockets, and JSON is a practical, hands-on guide that provides you with clear and pragmatic information to take advantage of the real power behind ...
**JAX-RS方式的RESTful Web Service开发详解** RESTful Web Service是一种基于HTTP协议的、无状态的、客户端-服务器交互模式,它利用HTTP方法(GET、POST、PUT、DELETE等)来实现对资源的操作。JAX-RS是Java API ...
This book, RESTful Java with JAX-RS 2.0, provides an in-depth tutorial on JAX-RS and shows how to get the most from this new API while adhering to the REST architectural style. I hope you enjoy the ...
标题"jax-rs jax-ws所需包,亲测可用"表明这个压缩包包含了用于开发Java RESTful Web服务(JAX-RS)和Java SOAP Web服务(JAX-WS)所需的库文件。这些库是Java应用程序进行Web服务交互的核心组件,确保了对HTTP协议...
在本篇文章中,我们将探讨如何利用Java SDK中的JAX-WS(Java API for XML Web Services)来创建和消费Web服务。 JAX-WS是Java平台上的一个标准组件,用于构建和部署SOAP Web服务。它简化了Web服务开发过程,提供了...
JSR 339(Java Specification Request 339)定义了Java API for RESTful Web Services (JAX-RS) 2.0 的最终规范。此版本是基于之前的JAX-RS 1.x版本的重大更新,它不仅增强了现有的功能,还引入了一些新的特性来更好...
rar中包含整个项目的源码和数据库生成脚本,采用jax-ws发布Web Service服务,支持java客户端和.Net客户端调用,数据库采用oracle10g,里面有创建数据库脚本文件createTable_Oracle10g.sql,由于Hibernate映射表中会...
总结来说,学习JAX-WSWebService开发意味着掌握Web服务的基本概念、JAX-WS的API用法,以及在MyEclipse这样的IDE中实现和调试Web服务的方法。通过这个过程,开发者可以熟练地创建、部署和调用Java平台上的Web服务,...
**JAX-RS CXF Web服务 RESTful CRUD与Spring集成详解** JAX-RS(Java API for RESTful Web Services)是Java平台上的一个标准,用于创建RESTful Web服务。REST是一种轻量级的架构风格,它以资源为中心,通过HTTP...