Webservice开发和使用指南1

 

1 .修改历史

Version
Change Time
 Author
Description
Effort Hours
0.1
2006-9-26
张乐雷
这是创建的最初的版本。将记录开发过程中遇到的各种问题。同时和其他相关的方法、资源等等。
0.5
0.2
2006-11-20
张乐雷
增加了,客户端、服务端生成方式、生成步骤、脚本测试方法
0.5
V1.0-final
2006-11-29
张乐雷
增加了:优秀文章、增加了发布服务的机制、优化axis工程支持命令和ant生成方式、去掉了代理类的生成、优化了文章结构。
2
V1.1
2006-12-7
张乐雷
增加webservice访问方式、启动服务的命令、cmd格式的文件、增加wsdd文件的发布方式、jws格式描述。
0.5
V1.2
2007-1-2
张乐雷
引入了日志跟踪方式、常见问题解答、修订了文件的格式、和97的邮件来往的问题技术交流、附件build.xml代码
2
V1.3
2007-1-25
张乐雷
对文章结构进行调整、引入的插件生成代码的方式、增加文档Roadmap
0.5
V1.4
2007-4-9
张乐雷
增加了工具ObjectWeb Lomboz生成客户端的方式。
0.3
V1.5
2007-7-11
张乐雷
增加了:获得wsdl文件的方式
1
V1.6
2007-7-26
张乐雷
删除:过时的资源;新增一些基本概念解释、分类、spring整合
0.5
V1.7
2007-8-9
张乐雷
删除:文档开发路线图、增加axis2解释
0.2

 
2 .目的和范围
这是一篇关于webservcie相关知识的介绍,其中主要包括:使用axis来进行开发和测试、axis和spring集成开发、aixs和web服务期集成开发、通过ObjectWeb Lomboz工具生成客户端和服务器代码方式等等。
核心内容包括: axis命令级别的详细解释、axis和spring相互结合开发指南、开发步骤的梳理和解释。本文只提到了Axis2的基本概念,详细知识,可以参见Axis2的相关文档。
这是重量级别的文章,对webservice知识点和axis的使用命令涉及到比较多。
2.1    主要内容包括
1、关于webservice的基本概念
2、Axis相关概念和命令。
3、开发和测试工具,代码实例。
4、开发客户端和服务器端的开发流程。
5、开发的详细的命令介绍。
6、如何测试的方法。
7、发布的步骤。
8、日志跟踪机制。
9、其他优秀文章。
10、 相关参考资料的介绍。
11、 其他文档的发布路线图。
2.2    本文的特色
1、 有助于理解webservice深层的设计思想。因为没有使用eclipse插件,强迫你每次可以看axis相关的知识,必然对相关的概念、代码的架构设计,可以对自动生成的代码的命名、调用细节,有比较深的理解,例如:Proxy模式的设计。
2、 增强你对工程的管理能力。 当生成代码时,你将每次手工的设置wsdl文件名称、路径名称等等,处处在影响这你。Ant是管理java的强大的工具,应用最广泛。无论多牛的开发图形工具,它的后台,对工程的管理,很多都是采用ant命令调用。
3、 提高你的测试能力。在附件中的工程,生成的代码支持Junit测试的java代码。通过测试,当你在你应用中调用时,通过copy、paste就可以搞定。
4、 增强你的思考能力。有些知识,网上是找不到的,这恰恰是你的机会,同时也是公司的机会,因为公司是单个人组成的。通过思考,通过动手,才能突破一些难点。例如:hibernate的Callback代码设计、强大的Template方法、多线程下数据同步和持久化、不同Ip之上数据表的操作访问,程序管理表和物理管理表的设计模式。网上,没人告诉你。不详细的它的源代码调用和相关规范,很难搞清楚。
5、 详细和大量的代码实例。这样可以方便你引用,这些所有的代码和build.xml文件,都是经过测试和应用过的。
2.3    关于实例工程
考虑到容量比较大,可能要自己到网站下载。参见:相关资源。
 
3 .Web Service基本概念
3.1    基本组成
由两部分组成
·SOAP--Web Service之间的基本通信协议。
·WSDL--Web Service描述语言,它定义了Web Service做什么,怎么做和查询的信息。
The Web Service Description Language is a specification authored by IBM and Microsoft, and supported by many other organizations. WSDL serves to describe Web Services in a structured way. A WSDL description of a service tells us, in a machine-understandable way, the interface to the service, the data types it uses, and where the service is located.
Handler
A reusable class which is responsible for processing a MessageContext in some custom way. The Axis engine invokes a series of Handlers whenever a request comes in from a client or a transport listener.
SOAP
The Simple Object Access Protocol (yes, despite the fact that it sometimes doesn't seem so simple, and doesn't have anything to do with objects... :)). You can read the SOAP 1.1 specification at http://www.w3.org/TR/SOAP. The W3C is currently in the midst of work on SOAP 1.2, under the auspices of the XML Protocol Group.
Provider
A provider is the "back-end" Handler which is responsible for actually performing the "meat" of the desired SOAP operation. Typically this means calling a method on some back-end service object. The two commonly used providers are RPCProvider and MsgProvider, both in the org.apache.axis.providers.java package.
 
3.2    什么是 Webservice?
Web 是使应用程序可以以与平台和编程语言无关的方式进行相互通信的一项技术。Web 服务是一个软件接口,它描述了一组可以在网络上通过标准化的 XML 消息传递访问的操作。它使用基于 XML 语言的协议来描述要执行的操作或者要与另一个 Web 服务交换的数据。一组以这种方式交互的 Web 服务在面向服务的体系结构(Service-Oriented Architecture,SOA)中定义了特殊的 Web 服务应用程序。
3.3    什么是SOAP?
SOAP(Simple Object Access Protocol )简单对象访问协议是在分散或分布式的环境中交换信息并执行远程过程调用的轻量级协议,是一个基于XML的协议。使用SOAP,不用考虑任何特定的传输协议(最常用的还是HTTP协议),可以允许任何类型的对象或代码,在任何平台上,以任何一种语言相互通信。
SOAP包括四个部分:SOAP封装(envelop),封装定义了一个描述消息中的内容是什么,是谁发送的,谁应当接受并处理它以及如何处理它们的框架;SOAP编码规则(encoding rules),用于表示应用程序需要使用的数据类型的实例;SOAP RPC表示(RPC representation),表示远程过程调用和应答的协定;SOAP绑定(binding),使用底层协议交换信息。
应用中比较关注的是envelop,由一个或多个Header和一个Body组成。
SOAP在可互操作的基础 Web 服务协议栈中的位置。
3.4    什么是WSDL?
WSDL(Web Service Description Language)Web服务器描述语言是用XML文档来描述Web服务的标准,是Web服务的接口定义语言,由Ariba、Intel、IBM、MS等共同提出,通过WSDL,可描述Web服务的三个基本属性:
·服务做些什么——服务所提供的操作(方法)
·如何访问服务——和服务交互的数据格式以及必要协议
·服务位于何处——协议相关的地址,如URL
WSDL文档以端口集合的形式来描述Web服务,WSDL 服务描述包含对一组操作和消息的一个抽象定义,绑定到这些操作和消息的一个具体协议,和这个绑定的一个网络端点规范。
WSDL在Web 服务概念性协议栈中的位置。
WSDL描述语言一般包含三部分
l         What部分:包括了type、message和portType元素
Type:定义了Web Service使用的数据结构(使用XML Schema定义)
Message:一个Message是SOAP的基本通信元素。每个Message可以有一个或多个Part,每个Part代表一个参数。
PortType:消息汇总为不同的操作并归入到一个被称为portType的实体中。一个portType代表一个接口(Web Service支持的操作集合),每个Web Service可以有多个接口,它们都使用portType表示。每个操作又包含了input和 output部分。
l         How部分:包含binding元素
binding元素将portType绑定到特定的通信协议上(如HTTP上的SOAP协议)
l         Where部分:由service元素组成
它将portType,binding以及Web Service实际的位置(URI)放在一起描述
 
3.5    什么是WSDD?
WSDD就是WEB服务分布描述(Web Service Deployment Descriptor), 它定义了WEB服务的接口,如服务名、提供的方法、方法的参数等信息。
 
3.6    什么是UDDI?
UDDI就是统一描述、发现和集成(Universal Description, Discovery, and Integration)。UDDI用于集中存放和查找WSDL描述文件,起着目录服务器的作用。
Web 服务中的角色、操作和构件。
服务提供者。从企业的角度看,这是服务的所有者。从体系结构的角度看,这是托管访问服务的平台。
服务请求者。从企业的角度看,这是要求满足特定功能的企业。从体系结构的角度看,这是寻找并调用服务,或启动与服务的交互的应用程序。服务请求者角色可以由浏览器来担当,由人或无用户界面的程序(例如,另外一个 Web 服务)来控制它。
服务注册中心。
这是可搜索的服务描述注册中心,服务提供者在此发布他们的服务描述。在静态绑定开发或动态绑定执行期间,服务请求者查找服务并获得服务的绑定信息(在服务描述中)。对于静态绑定的服务请求者,服务注册中心是体系结构中的可选角色,因为服务提供者可以把描述直接发送给服务请求者。同样,服务请求者可以从服务注册中心以外的其它来源得到服务描述,例如本地文件、FTP 站点、Web 站点、广告和服务发现(Advertisement and Discovery of Services,ADS)或发现 Web 服务(Discovery of Web Services,DISCO)。
 
3.7    客户端和服务器端
3.7.1    客户端
根据wsdl文件生成的一组相关的客户端类。然后,通过wsdl地址可以访问其他系统或应用所提供的功能。
通常Web Service可以有三种类型的客户
l         商业伙伴(Business Partner)--包括分发商,零售商以及大型消费者)
此类客户通过SOAP、WSDL、ebXML、UDDI等XML技术与Web Service连接
l         瘦客户--包括Web浏览器、PDA以及无线设备
该类客户通常经由轻量协议(如HTTP)与Web Service连接
l         肥客户--包括Applet、各类应用以及现存系统
通常使用重量级协议(如IIOP)连接Web Service
3.7.2  服务器端
也就是对外提供的一个接口(本质上就是:方法),让其他不同的系统或应用可以来进行访问,本系统的功能。它包括的内容:
1.一个对外接口
2.该接口的实现类
3.一个部署描述文件,描述这个服务(接口)。部署文件中的服务的信息,是可以通过axis的命令来生成的。
这些文件都是将包含在一个完整的web应用中的,当启动web容器的时候,就可以其他系统进行访问了。
4 .Axis概念、命令和方法
4.1    什么是axis?
Apache Axis 是Apache WebService项目中的子项目,其最初起源于IBM的"SOAP4J",应该属于最早的一批用于构造基于SOAP应用的Framework。 目前Apache Axis已经发展到了第三代,其核心是一个SOAP处理器,用于开发包括客户端,服务器端,SOAP Gateway等各种应用。事实上Apache Axis在了1.0版后,其发行版本还包括了完整的J2EE服务器插件, WSDL支持和生成,TCP/IP监视器等组件,从这个意义上来说Apahce Axis已不仅仅是个SOAP框架了,它包含了除了UDDI外对整个Web Service协议栈(Protocol Stack)的支持。
Axis本质上就是一个SOAP引擎(Apache Axis is an implementation of the SOAP),提供创建服务器端、客户端和网关SOAP操作的基本框架。但Axis并不完全是一个SOAP引擎,它还包括:
是一个独立的SOAP服务器。
是一个嵌入Servlet引擎(例如Tomcat)的服务器。
支持WSDL。
提供转化WSDL为Java类的工具。
提供例子程序。
提供TCP/IP数据包监视工具。
4.2    什么时Axis2?
A new architecture for Axis was introduced during the August 2004 Summit in Colombo, Sri Lanka. The new architecture on which Axis2 is based on is more flexible, efficient and configurable in comparison to Axis1.x architecture. Some well established concepts from Axis 1.x, like handlers etc., have been preserved in the new architecture.
Apache Axis2 not only supports SOAP 1.1 and SOAP 1.2, but it also has integrated support for the widely popular REST style of Web services. The same business logic implementation can offer both a WS-* style interface as well as a REST style interface simultaneously.
Apache Axis2 is more efficient, more modular and more XML-oriented than the older version. It is carefully designed to support the easy addition of plug-in "modules" that extend their functionality for features such as security and reliability. The Modules currently available or under development include:
Apache Axis2 is built on Apache AXIOM, a new high performant, pull-based XML object model.
Axis2 comes with many new features, enhancements and industry specification implementations. The key features offered are as follows:
·         Speed - Axis2 uses its own object model and StAX (Streaming API for XML) parsing to achieve significantly greater speed than earlier versions of Apache Axis.
·         Low memory foot print- Axis2 was designed ground-up keeping low memory foot print in mind.
·         AXIOM - Axis2 comes with its own light-weight object model, AXIOM, for message processing which is extensible, highly performant and is developer convenient.
·         Hot Deployment - Axis2 is equipped with the capability of deploying Web services and handlers while the system is up and running. In other words, new services can be added to the system without having to shut down the server. Simply drop the required Web service archive into the services directory in the repository, and the deployment model will automatically deploy the service and make it available for use.
·         Asynchronous Web services - Axis2 now supports asynchronous Web services and asynchronous Web services invocation using non-blocking clients and transports.
·         MEP Support - Axis2 now comes handy with the flexibility to support Message Exchange Patterns (MEPs) with in-built support for basic MEPs defined in WSDL 2.0.
·         Flexibility - The Axis2 architecture gives the developer complete freedom to insert extensions into the engine for custom header processing, system management, and anything else you can imagine.
·         Stability - Axis2 defines a set of published interfaces which change relatively slowly compared to the rest of Axis.
·         Component-oriented Deployment - You can easily define reusable networks of Handlers to implement common patterns of processing for your applications, or to distribute to partners.
·         Transport Framework - We have a clean and simple abstraction for integrating and using Transports (i.e., senders and listeners for SOAP over various protocols such as SMTP, FTP, message-oriented middleware, etc), and the core of the engine is completely transport-independent.
·         WSDL support - Axis2 supports the Web Service Description Language, version 1.1 and 2.0, which allows you to easily build stubs to access remote services, and also to automatically export machine-readable descriptions of your deployed services from Axis2.
·         Add-ons - Several Web services specifications have been incorporated including WSS4J for security (Apache Rampart), Sandesha for reliable messaging, Kandula which is an encapsulation of WS-Coordination, WS-AtomicTransaction and WS-BusinessActivity.
·         Composition and Extensibility - Modules and phases improve support for composability and extensibility. Modules support composability and can also support new WS-* specifications in a simple and clean manner. They are however not hot deployable as they change the overall behavior of the system.
 
4.3    文件格式介绍
1.    JWS (Java Web Service) Files - Instant Deployment 它是格式正确的 java 文件,文件的后缀是 .jws 。如果发布,可以直接把它放到发布的工程根目录下。它不能指定包名。如果引用其他类,只能其他类也不能指定包名。
 
4.4    Axis工具简单操作命令和基本概念
1.显示帮助信息:j ava -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java
生成客户端代码实例: 前提是必须的在dos切换到axis的lib目录。然后执行这个命令。j java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java -a -H -d -s -t -S true -p com.qnuse.zj114.webservice http://134.96.71.58/QnInterface/HzGsyj.asmx?wsdl
2.WSDL2Java工具用于从WSDL文件生成客户端存根(stub)代码,服务端框架(skeleton)代码以及WSDL中的数据类型文件(生成与之对应的Java代码)。
3.server-config.wsdd记录了axis已发布的Web服务的描述信息。
4.5    Axis工具wsdl2java命令参数解释
1.    -N, --NStoPkg <argument>=<value> 
mapping of namespace to package
2.    -a, --all 
 generate code for all elements, even unreferenced ones
3.    -p, --package <argument>
         override all namespace to package mappings, use this package
          name instead
4.    -h, --help
         print this message and exit
5.     -v, --verbose
         print informational messages
6.    -t, --testCase
        web service 发行 junit 测试用例类
 
4.6    server-config.wsdd文件解释
它是AXIS的配置文件,所有发布的服务都会在里面描述。
如果还没有server-config.wsdd文件,可以先写一个发布描述文件,然后用axis来生成,以后需要发布新的服务,直接往里面添加即可。
比如有一个HelloWorld.class类,里面有sayHello(String msg)方法,想把它发布webservices,先写发布文件deploy.wsdd文件,即SOAP服务发布描述文件deploy.wsdd
http://xml.apache.org/axis/wsdd/" xmlns:java=" http://xml.apache.org/axis/wsdd/providers/java">
然后,进入dos窗口,执行下列命令:
java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient deploy.wsdd 即可生成server-config.wsdd文件。
如果想发布新的服务,也可以直接往server-config.wsdd里面添加service描述。
 
4.7    查看发布的webservice
1.首先要查看工程的web.xml的配置情况,例如:zj114boss的web.xml中对axis的配置是:
    < servlet >
       < servlet-name > axis </ servlet-name >      < servlet-class > org.apache.axis.transport.http.AxisServlet </ servlet-class >
       < load-on-startup > 5 </ load-on-startup >
    </ servlet >
    < servlet-mapping >
       < servlet-name > axis </ servlet-name >
       < url-pattern > /webservice/* </ url-pattern >
    </ servlet-mapping >
2.启动容器,或看测试环境。在IE上键入: http://134.96.71.27:8080/zj114boss-1.0/webservice 你将看到全部发布的service。
3.看详细的wsdl内容。例如:查看生产环境 http://134.96.71.73/usboss/webservice/ssoService?wsdl
 
4.8    序列化与反序列化
序列化/反序列化器在英文中的对应翻译是Serializer/Deserializer,一个序列化器的功能是遵循一定的映射规则和编码风格,将一种类型的JAVA对象通过某种特定的机制,转换成为XML描述的形式;反序列化器的功能是序列化器所做工作的逆操作,两者相辅相成,成对出现。Axis中的序列化/反序列化器采用设计范式中的工厂模式,每一个Serializer唯一对应一个SerializerFactory;每一个Deserializer唯一对应一个DeserializerFactory。
Axis已经为开发者提供了丰富的序列化/反序列化器,对于java的基本数据类型,绝大部分常用的容器类(比如数组类型,Vector类型等)都提供了实现,特别是提供了对W3C的DOM对象(比如Document, Element等)和符合Bean规范的JAVA对象提供了功能完善的序列化/反序列化器,但对于一些特殊类型的对象,需要通过Web服务进行传递,我们不得不开发自己的序列化/反序列化器。
4.9    Axis相比Soap v2的优点
Axis是第三代Apache SOAP的实现,从2000年起,SOAP v2开发小组开始讨论如何让Axis更加灵活、可配置,以及能够处理SOAP和来自W3C的各种XML标准。通过不断地讨论和代码编写,Axis目前相比SOAP V2取得了如下成果:
速度提高。 Axis通过基于事件的SAX对XML文档进行处理,从而在速度和效率上比Apache SOAP有所提高。
灵活性提高。
稳定性提高。
提供面向组件的部署。
提供一个简洁的传输抽象框架。其核心引擎完全于传输方式独立。从而使基于何种协议传输的选择更加灵活。
支持WSDL。包括WSDL和客户端代码生成等。
4.10       性能
axis使用1.3版,xfire使用1.0版。xfire使用spring的servicesbean方式发布web服务。web services客户端使用delphi 7开发,分别对通过axis发布的web services及通过xfire发布的web services进行1万次连续调用(即一次获取wsdl后连续使用)。axis调用1万次内存占用48M,用时1分40秒 xfire调用1万次内存占用38M,用时1分05秒。使用axis2测试时发现通过axis2发布的web services 在delphi中可以导入,但是运行时会报错。
4.11       更多
5 开发和测试工具
5.1    开发工具
1.      采用 axis的相关eclipse插件、或者其他形式的插件。例如:ObjectWeb Lomboz An Eclipse based IDE for J2EE Development
2.      采用 axis代码本身工具(dos命令行或ant脚本)来开发。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值