webservice培训.ppt_第1页
webservice培训.ppt_第2页
webservice培训.ppt_第3页
webservice培训.ppt_第4页
webservice培训.ppt_第5页
已阅读5页,还剩25页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Webservice培训 顾邹阳 Webservice简介 我自己理解Webservice就是调用其他网站的资源的一种方法 主要分成 Web服务和调用即服务端与客户端Webservice是一种能简单快速实现服务与客户端调用的一种可以由多种语言实现的方法 可以是java C 等 具体的工作原理 服务器端发布一个服务 然后客户端通过服务器端发布出来的wsdl来进行访问现在比较流行的java开发包有axis axis2 CXF xfire具体开发的时候所用的工具我这边是eclipse发布服务器我这边用的是Tomcat5 5 具体的程序开发axis 1 axis所要用到的包为 axis jar axis ant jar commons discovery 0 2 jar commons logging 1 0 4 jar jaxrpc jar log4j 1 2 8 jar saaj jar wsdl4j 1 5 1 jar具体的开发的时候要先配制web xml要在web xml里加入如下内容 AxisServletorg apache axis transport http AxisServletAdminServletorg apache axis transport http AdminServlet100 具体的程序开发axis SOAPMonitorServiceorg apache axis monitor SOAPMonitorServiceSOAPMonitorPort5001100AxisServlet servlet AxisServletAxisServlet jwsAxisServlet services SOAPMonitorService SOAPMonitor 具体的程序开发axis 具体举例 服务器端的类 publicclassSayHello publicstaticStringsayHello Stringname return Hello name 这个方法要定义成public要有返回类型 参数和具体的返回 具体的程序开发axis 发布 要在server config wsdd里进行发布我们在server config wsdd里找到在这之后添加我们自己的要进行发布的方法这里定义的方法名为 SayHello 可以随便 如果是简单的方法一般格式为RPC就可以了com land axis server SayHello这个就是类的路径配制完保存后我们就可以启动TOMCAT了我这里工程名为axisws tomcat的端口为9001所以得到以下路径http localhost 9001 axisws services大家可以看到以下页面 具体的程序开发axis 点击SayHello 可以看到以下页面 具体的程序开发axis 具体的程序开发axis 如果看到以上的页面说明你的发布成功了那么我们就可以再去写一个客户端程序如下 MalformedURLException importjava rmi RemoteException importjavax xml rpc ServiceException importorg apache axis client Call importorg apache axis client Service publicclassSayHelloClient publicstaticvoidmain String args TODO自动生成方法存根Stringendpoint http localhost 9001 axisws services SayHello Serviceservice newService 回调返回值在这里进行序列化Callcall try call Call service createCall 定义回调用的callcall setTargetEndpointAddress URL endpoint 把要调用的方法的地址放在这里call setOperationName sayHello 这个是方法名Stringname 顾邹阳 这个是sayHello方法的参数定义Stringobj String call invoke newObject name 开始调用obj是返回值System out println 返回值 obj catch ServiceExceptione e printStackTrace catch MalformedURLExceptione e printStackTrace catch RemoteExceptione e printStackTrace 我们执行一下如果成功的话会在控制台出现如下的打印返回值 Hello顾邹阳 具体的程序开发axis2 2 AXIS2用POJO实现0配置的WebServiceAxis2支持SOAP1 1和SOAP1 2 我这里只讲几个简单的例子Axis2的下载和安装可以从如下的网址下载Axis2的最新版本 http ws apache org axis2 我使用了目前Axis2的最新版本1 4 1 可以下载如下两个zip包 axis2 1 4 1 bin zipaxis2 1 4 1 war zip其中axis2 1 4 1 bin zip文件中包含了Axis2中所有的jar文件 axis2 1 4 1 war zip文件用于将WebService发布到Web容器中 将axis2 1 4 1 war zip文件解压到相应的目录 将目录中的axis2 war文件放到 webapps目录中并启动Tomcat 在浏览器地址栏中输入如下的URL http localhost 9001 axis2 如果在浏览器中显示出如图1所示的页面 则表示Axis2安装成功 具体的程序开发axis2 具体的程序开发axis2 编写和发布WebService在Axis2中不需要进行任何的配置 就可以直接将一个简单的POJO发布成WebService 其中POJO中所有的public方法将被发布成WebService方法 下面我们来实现一个简单的POJO 代码如下 publicclassTestService publicStringsayHello Stringname 这个方法是有参数的 return Hello name publicintgetRandom 这个方法是没有参数的 returnnewjava util Random nextInt 1000 在TestService类中有两个方法 由于这两个方法都是public方法 因此 它们都将作为WebService方法被发布 编译TestService类后 将TestService class文件放到 webapps axis2 WEB INF pojo目录中 如果没有pojo目录 则建立该目录 现在我们已经成功将TestService类发布成了WebService 在浏览器地址栏中输入如下的URL http localhost 9001 axis2 services listServices 具体的程序开发axis2 具体的程序开发axis2 在浏览器地址栏中输入如下的两个URL来分别测试sayHello和getRandom方法 http localhost 9001 axis2 services TestService sayHello name bill http localhost 9001 axis2 services TestService getRandom 具体的程序开发axis2 在编写 发布和测试配置的WebService时应注意如下几点 1 POJO类不能使用package关键字声明包 2 Axis2在默认情况下可以热发布WebService 也就是说 将WebService的 class文件复制到pojo目录中时 Tomcat不需要重新启动就可以自动发布WebService 如果想取消Axis2的热发布功能 可以打开 webapps axis2 WEB INF conf axis2 xml 找到如下的配置代码 true将true改为false即可 要注意的是 Axis2在默认情况下虽然是热发布 但并不是热更新 也就是说 一旦成功发布了WebService 再想更新该WebService 就必须重启Tomcat 这对于开发人员调试WebService非常不方便 因此 在开发WebService时 可以将Axis2设为热更新 在axis2 xml文件中找到false 将false改为true即可 3 在浏览器中测试WebService时 如果WebService方法有参数 需要使用URL的请求参数来指定该WebService方法参数的值 请求参数名与方法参数名要一致 例如 要测试sayHello方法 请求参数名应为name 如上面的URL所示 4 发布WebService的pojo目录只是默认的 如果读者想在其他的目录发布WebService 可以打开axis2 xml文件 并在元素中添加如下的子元素 上面的配置允许在 webapps axis2 WEB INF my目录中发布WebService 例如 将本例中的TestService class复制到my目录中也可以成功发布 但要删除pojo目录中的TestService class 否则WebService会重名 具体的程序开发axis2 实现调用WebService的客户端程序importjavax xml namespace QName importorg apache axis2 addressing EndpointReference importorg apache axis2 client Options importorg apache axis2 rpc client RPCServiceClient publicclassRPCClient publicstaticvoidmain String args throwsException 使用RPC方式调用WebServiceRPCServiceClientserviceClient newRPCServiceClient Optionsoptions serviceClient getOptions 指定调用WebService的URLEndpointReferencetargetEPR new 具体的程序开发axis2 EndpointReference http localhost 9001 axis2 services TestService options setTo targetEPR 指定getGreeting方法的参数值Object opAddEntryArgs newObject gzy 参数的内容 指定getGreeting方法返回值的数据类型的Class对象Class classes newClass String class 返回值为String 指定要调用的getGreeting方法及WSDL文件的命名空间QNameopAddEntry newQName http ws apache org axis2 sayHello 调用getGreeting方法并输出该方法的返回值System out println serviceClient invokeBlocking opAddEntry opAddEntryArgs classes 0 这里有一个参数 下面是调用getPrice方法的代码 这些代码与调用getGreeting方法的代码类似classes newClass int class 返回值为intopAddEntry newQName http ws apache org axis2 getRandom 具体的方法名http ws apache org axis2为命名空间System out println serviceClient invokeBlocking opAddEntry newObject classes 0 参数为空时不能为null只能为newObject 具体的程序开发CXF CXF是不需要web就能直接发布的一个webservice工具下面我们直接用例子来说明CXF的使用我这里用到的包为apache cxf 2 1 8 zip1先建立一个HelloWorld的工程Java运行环境要1 5以上然后选择构建路径导入外部jar如图 具体的程序开发CXF 把apache cxf 2 1 8里面解压之后的lib下面的包全部导入进来 我在这里是先在工程下建立了一个lib把所有的lib都导入到了里面 具体的程序开发CXF 建立一个接口接口名叫HelloWold 具体的程序开发CXF packagetest importjavax jws WebService WebServicepublicinterfaceHelloWorld publicStringsayHello Stringname 具体的程序开发CXF 再添加一个实现类packagetest importjavax jws WebService WebServicepublicclassHelloWorldImplimplementsHelloWorld publicStringsayHello Stringname System out println sayhelloiscalled return Hello name 这里接口和实现类里都有一个标注 WebService这是为了把这个类发布成webservice所要用到的在标注后还要导入importjavax jws WebService 具体的程序开发CXF 然后我们建立一个webservice启动的类packagetest importorg apache cxf endpoint Server importorg apache cxf jaxws JaxWsServerFactoryBean publicclassMainServer publicstaticvoidmain String args JaxWsServerFactoryBeanfactory newJaxWsServerFactoryBean factory setServiceClass HelloWorldImpl class 这里的是实现类而不是接口factory setAddress http localhost 8080 HelloWorld Serverserver

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论