java调用webService的各种方法.doc_第1页
java调用webService的各种方法.doc_第2页
java调用webService的各种方法.doc_第3页
java调用webService的各种方法.doc_第4页
java调用webService的各种方法.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

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

文档简介

java调用webService的各种方法java 调用webservice的各种方法总结 现在webservice加xml技术已经逐渐成熟,但要真正要用起来还需时日!一、利用jdk web服务api实现,这里使用基于 SOAP message 的 Web 服务 1.首先建立一个Web services EndPoint: package Hello; import javax.jws.WebService; import javax.jws.WebMethod; import javax.xml.ws.Endpoint; WebService public class Hello WebMethod public String hello(String name) return Hello, + name + n; public static void main(String args) / create and publish an endpoint Hello hello = new Hello(); Endpoint endpoint = Endpoint.publish(http:/localhost:8080/hello, hello); 2.使用 apt 编译 Hello.java(例:apt -d 存放编译后的文件目录 Hello.java ) ,会生成 jaws目录 3.使用java Hello.Hello运行,然后将浏览器指向http:/localhost:8080/hello?wsdl就会出现下列显示 4.使用wsimport 生成客户端 使用如下:wsimport -p . -keep http:/localhost:8080/hello?wsdl 这时,会在当前目录中生成如下文件: 5.客户端程序: 1class HelloClient 2public static void main(String args) 3 HelloService service = new HelloService(); 4 Hello helloProxy = service.getHelloPort(); 5 String hello = helloProxy.hello(你好); 6 System.out.println(hello); 7 89 以上方法还稍显繁琐,还有更加简单的方法二、使用xfire,我这里使用的是myeclipse集成的xfire进行测试的 利用xfire开发WebService,可以有三种方法: 1一种是从javabean 中生成; 2 一种是从wsdl文件中生成; 3 还有一种是自己建立webservice 步骤如下: 用myeclipse建立webservice工程,目录结构如下: 首先建立webservice接口, 代码如下: 1package com.myeclipse.wsExample;2/Generated by MyEclipse34public interface IHelloWorldService 5 6 public String example(String message);7 8 接着实现这个借口: 1package com.myeclipse.wsExample;2/Generated by MyEclipse34public class HelloWorldServiceImpl implements IHelloWorldService 5 6 public String example(String message) 7 return message;8 9 10 修改service.xml 文件,加入以下代码: 12 HelloWorldService3 4 com.myeclipse.wsExample.IHelloWorldService5 6 7 com.myeclipse.wsExample.HelloWorldServiceImpl8 9 wrapped10 literal11 application12 把整个项目部署到tomcat服务器中 ,打开浏览器,输入http:/localhost:8989/HelloWorld/services/HelloWorldService?wsdl,可以看到如下: 然后再展开HelloWorldService后面的wsdl可以看到:客户端实现如下: 1package com.myeclipse.wsExample.client;23import .MalformedURLException;4import .URL;56import org.codehaus.xfire.XFireFactory;7import org.codehaus.xfire.client.Client;8import org.codehaus.xfire.client.XFireProxyFactory;9import org.codehaus.xfire.service.Service;10import org.codehaus.xfire.service.binding.ObjectServiceFactory;1112import com.myeclipse.wsExample.IHelloWorldService;1314public class HelloWorldClient 15public static void main(String args) throws MalformedURLException, Exception 16/ TODO Auto-generated method stub17Service s=new ObjectServiceFactory().create(IHelloWorldService.class);18XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire();19String url=http:/localhost:8989/HelloWorld/services/HelloWorldService;2021 try22 23 24 IHelloWorldService hs=(IHelloWorldService) xf.create(s,url);25 String st=hs.example(zhangjin);26 System.out.print(st);27 28 catch(Exception e)29 30 e.printStackTrace();31 32 333435 这里再说点题外话,有时候我们知道一个wsdl地址,比如想用java客户端引用.net 做得webservice,使用myeclipse引用,但是却出现无法通过验证的错误,这时我们可以直接在类中引用,步骤如下: 1public static void main(String args) throws MalformedURLException, Exception 2 / TODO Auto-generated method stub3 Service s=new ObjectServiceFactory().create(IHelloWorldService.class);4 XFireProxyFactory xf=new XFireProxyFactory(XFireFactory.newInstance().getXFire();5 6 7/远程调用.net开发的webservice8Client c=new Client(new URL(/webservices/qqOnlineWebService.asmx?wsdl);9 Object o=c.invoke(qqCheckOnline, new String531086641,591284436);10 11/调用.net本机开发的webservice12Client c1=new Client(new URL(http:/localhost/zj/Service.asmx?wsdl);13Object o1=c1.invoke(HelloWorld,new String);14 15 三、使用axis1.4调用webservice方法 前提条件:下载axis1.4包和tomcat服务器 ,并将axis文件夹复制到tomcat服务器的webapp文件夹中 这里我就说一下最简单的方法: 首先建立一个任意的java类(例如:HelloWorld.java),复制到axis文件夹下,将其扩展名改为jws,然后重新启动tomcat,在浏览器中输入http:/localhost:8989/axis/HelloWorld.jws?wsdl,就会得到一个wsdl文件,其客户端调用方法如下: 1import javax.xml.rpc.Service;2import javax.xml.rpc.ServiceException;3import javax.xml.rpc.ServiceFactory;45import .MalformedURLException;6import .URL;7import java.rmi.RemoteException;89import space.QName;1011public class TestHelloWorld 121314 public static void main(String args) throws MalformedURLException, ServiceException, RemoteException 15 / TODO Auto-generated method stub16 17 String wsdlUrl =http:/localhost:8989/axis/HelloWorld.jws?wsdl;18 String nameSpaceUri =http:/localhost:8989/axis/HelloWorld.jws;19 String serviceName = HelloWorldService;20 String portName = HelloWorld;21 22 ServiceFactory serviceFactory = ServiceFactory.newInstance();23 Service afService =serviceFactory.createService(new URL(wsdlUrl),new QName(nameSpaceUri, serviceName);24 HelloWorldInterface proxy = (HelloWorldInterface)afService.getPort(new QName(nameSpaceUri, portName),HelloWorldInterface.class);25 System.out.println(return value is +proxy.getName(john) ) ;26 27 282930四、使用axis2开发webservice(这里首先感谢李宁老师) 使用axis2 需要先下载 axis2-1.4.1-bin.zip axis2-1.4.1-war.zip/axis2/ 同理,也需要将axis2复制到webapp目录中在axis2中部署webservice有两种方法, 第一种是pojo方式,这种方式比较简单,但是有一些限制,例如部署的类不能加上包名 第二种方式是利用xml发布webservice,这种方法比较灵活,不需要限制类的声明 下面分别说明使用方法: 1.pojo方式:在Axis2中不需要进行任何的配置,就可以直接将一个简单的POJO发布成WebService。其中POJO中所有的public方法将被发布成WebService方法。先实现一个pojo类: 1public class HelloWorld2 public String getName(String name)3 4 return 你好 + name;5 6 public int add(int a,int b)7 8 return a+b;9 1011 由于这两个方法都是public类型,所以都会发布成webservice。编译HelloWorld类后,将HelloWorld.class文件放到%tomcat%webappsaxis2WEB-INFpojo目录中(如果没有pojo目录,则建立该目录),然后打开浏览器进行测试:输入一下url: http:/localhost:8080/axis2/services/listServices会列出所有webservice这是其中的两个webservice列表,接着,在客户端进行测试:首先可以写一个封装类,减少编码,代码如下: 1package MZ.GetWebService;2import space.QName;34import org.apache.axis2.AxisFault;5import org.apache.axis2.addressing.EndpointReference;6import org.apache.axis2.client.Options;7import org.apache.axis2.rpc.client.RPCServiceClient;8910public class GetWSByAxis2 11 private static String EndPointUrl;12 private static String QUrl=/axis2;13 private QName opAddEntry; 14 public String WSUrl;15 public RPCServiceClient setOption() throws AxisFault16 17 RPCServiceClient serviceClient = new RPCServiceClient();18 Options options = serviceClient.getOptions();19 EndpointReference targetEPR = new EndpointReference(WSUrl);20 options.setTo(targetEPR);21 return serviceClient;22 23 24 public QName getQname(String Option)25 26 return new QName (QUrl,Option);27 28 /返回String29 public String getStr(String Option) throws AxisFault30 31 RPCServiceClient serviceClient =this.setOption(); 32 33 opAddEntry =this.getQname(Option);34 35 String str = (String) serviceClient.invokeBlocking(opAddEntry, 36 new Object, new ClassString.class )0;37 return str;38 39/ 返回一维String数组40 public String getArray(String Option) throws AxisFault41 42 RPCServiceClient serviceClient =this.setOption(); 43 44 opAddEntry =this.getQname(Option);45 46 String strArray = (String) serviceClient.invokeBlocking(opAddEntry, 47 new Object, new ClassString.class )0;48 return strArray;49 50 /从WebService中返回一个对象的实例51 public Object getObject(String Option,Object o) throws AxisFault52 53 RPCServiceClient serviceClient =this.setOption(); 54 QName qname=this.getQname(Option);55 Object object = serviceClient.invokeBlocking(qname, new Object,n

温馨提示

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

评论

0/150

提交评论