`

Axis2开发WebService客户端 的3种方式

    博客分类:
  • Java
 
阅读更多

第一RPC方式,不生成客户端代码

第二,document方式,不生成客户端代码

第三,用wsdl2java工具,生成客户端方式调用

    package samples.quickstart.client;

    import javax.xml.namespace.QName;
    import org.apache.axiom.om.OMAbstractFactory;
    import org.apache.axiom.om.OMElement;
    import org.apache.axiom.om.OMFactory;
    import org.apache.axiom.om.OMNamespace;
    import org.apache.axis2.AxisFault;
    import org.apache.axis2.addressing.EndpointReference;
    import org.apache.axis2.client.Options;
    import org.apache.axis2.client.ServiceClient;
    import org.apache.axis2.rpc.client.RPCServiceClient;
    import samples.quickstart.StockQuoteServiceStub;
    import samples.quickstart.xsd.GetPrice;
    import samples.quickstart.xsd.GetPriceResponse;

    public class StockQuoteClient {

      /**
       * 方法一:
       * 应用rpc的方式调用 这种方式就等于远程调用,
       * 即通过url定位告诉远程服务器,告知方法名称,参数等, 调用远程服务,得到结果。
       * 使用 org.apache.axis2.rpc.client.RPCServiceClient类调用WebService
       *
        【注】:
        
            如果被调用的WebService方法有返回值 应使用 invokeBlocking 方法 该方法有三个参数
              第一个参数的类型是QName对象,表示要调用的方法名;
              第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];
                当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。
              第三个参数表示WebService方法的 返回值类型的Class对象,参数类型为Class[]。
            
            
            如果被调用的WebService方法没有返回值 应使用 invokeRobust 方法
              该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。

            在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,
            也就是 <wsdl:definitions>元素的targetNamespace属性值。
       *
       */
      public static void testRPCClient() {
        try {
          // axis1 服务端
    // String url = "http://localhost:8080/StockQuote/services/StockQuoteServiceSOAP11port?wsdl";
          // axis2 服务端
          String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService?wsdl";

          // 使用RPC方式调用WebService
          RPCServiceClient serviceClient = new RPCServiceClient();
          // 指定调用WebService的URL
          EndpointReference targetEPR = new EndpointReference(url);
          Options options = serviceClient.getOptions();
          //确定目标服务地址
          options.setTo(targetEPR);
          //确定调用方法
          options.setAction("urn:getPrice");

          /**
           * 指定要调用的getPrice方法及WSDL文件的命名空间
           * 如果 webservice 服务端由axis2编写
           * 命名空间 不一致导致的问题
           * org.apache.axis2.AxisFault: java.lang.RuntimeException: Unexpected subelement arg0
           */
          QName qname = new QName("http://quickstart.samples/xsd", "getPrice");
          // 指定getPrice方法的参数值
          Object[] parameters = new Object[] { "13" };
          
          // 指定getPrice方法返回值的数据类型的Class对象
          Class[] returnTypes = new Class[] { double.class };

          // 调用方法一 传递参数,调用服务,获取服务返回结果集
          OMElement element = serviceClient.invokeBlocking(qname, parameters);
          //值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。
          //我们可以对之灵活应用,下面我取第一个元素值,并打印之。因为调用的方法返回一个结果
          String result = element.getFirstElement().getText();
          System.out.println(result);

          // 调用方法二 getPrice方法并输出该方法的返回值
          Object[] response = serviceClient.invokeBlocking(qname, parameters, returnTypes);
          // String r = (String) response[0];
          Double r = (Double) response[0];
          System.out.println(r);

        } catch (AxisFault e) {
          e.printStackTrace();
        }
      }

      /**
       * 方法二: 应用document方式调用
       * 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合
       */
      public static void testDocument() {
        try {
          // String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService";
          String url = "http://localhost:8080/StockQuote/services/StockQuoteServiceSOAP11port?wsdl";

          Options options = new Options();
          // 指定调用WebService的URL
          EndpointReference targetEPR = new EndpointReference(url);
          options.setTo(targetEPR);
          // options.setAction("urn:getPrice");

          ServiceClient sender = new ServiceClient();
          sender.setOptions(options);
          
          
          OMFactory fac = OMAbstractFactory.getOMFactory();
          String tns = "http://quickstart.samples/";
          // 命名空间,有时命名空间不增加没事,不过最好加上,因为有时有事,你懂的
          OMNamespace omNs = fac.createOMNamespace(tns, "");

          OMElement method = fac.createOMElement("getPrice", omNs);
          OMElement symbol = fac.createOMElement("symbol", omNs);
          // symbol.setText("1");
          symbol.addChild(fac.createOMText(symbol, "Axis2 Echo String "));
          method.addChild(symbol);
          method.build();
          
          OMElement result = sender.sendReceive(method);

          System.out.println(result);

        } catch (AxisFault axisFault) {
          axisFault.printStackTrace();
        }
      }

     /**
      * 为SOAP Header构造验证信息,
      * 如果你的服务端是没有验证的,那么你不用在Header中增加验证信息
      *
      * @param serviceClient
      * @param tns 命名空间
      * @param user
      * @param passwrod
      */
      public void addValidation(ServiceClient serviceClient, String tns , String user, String passwrod) {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace(tns, "nsl");
        OMElement header = fac.createOMElement("AuthenticationToken", omNs);
        OMElement ome_user = fac.createOMElement("Username", omNs);
        OMElement ome_pass = fac.createOMElement("Password", omNs);
        
        ome_user.setText(user);
        ome_pass.setText(passwrod);
        
        header.addChild(ome_user);
        header.addChild(ome_pass);

        serviceClient.addHeader(header);
      }

      
      /**
       * 方法三:利用axis2插件生成客户端方式调用
       *
       */
      public static void testCodeClient() {
        try {
          String url = "http://localhost:8080/axis2ServerDemo/services/StockQuoteService";
          StockQuoteServiceStub stub = new StockQuoteServiceStub(url);
          GetPrice request = new GetPrice();
          request.setSymbol("ABCD");
          GetPriceResponse response = stub.getPrice(request);
          System.out.println(response.get_return());
        } catch (org.apache.axis2.AxisFault e) {
          e.printStackTrace();
        } catch (java.rmi.RemoteException e) {
          e.printStackTrace();
        }

      }

      public static void main(String[] args) {
         StockQuoteClient.testRPCClient();
    // StockQuoteClient.testDocument();
        // StockQuoteClient.testCodeClient();

      }
    }

 

wsdl2java 用于根据WSDL生成相应的服务端和客户端代码的生成工具。
命令行格式为:WSDL2Java [options] -uri <url or path> : A url or path to a WSDL

例如:

wsdl2java -uri http://localhost:8080/cxfService_0617/services/Hellows?wsdl -s -o build\client

 

其中常用的options具体如下:
-o <path> : 指定生成代码的输出路径
-a : 生成异步模式的代码
-s : 生成同步模式的代码
-p <pkg> : 指定代码的package名称
-l <languange> : 使用的语言(Java/C) 默认是java
-t : 为代码生成测试用例
-ss : 生成服务端代码 默认不生成
-sd : 生成服务描述文件 services.xml,仅与-ss一同使用
-d <databinding> : 指定databingding,例如,adb,xmlbean,jibx,jaxme and jaxbri
-g : 生成服务端和客户端的代码
-pn <port_name> : 当WSDL中有多个port时,指定其中一个port
-sn <serv_name> : 选择WSDL中的一个service
-u : 展开data-binding的类
-r <path> : 为代码生成指定一个repository
-ssi : 为服务端实现代码生成接口类
-S : 为生成的源码指定存储路径
-R : 为生成的resources指定存储路径
–noBuildXML : 输出中不生成build.xml文件
–noWSDL : 在resources目录中不生成WSDL文件
–noMessageReceiver : 不生成MessageReceiver类

 

 

 

 

 

 

分享到:
评论
7 楼 kaihop 2017-12-05  
很好,值得学习
6 楼 qwertyuiopqaz 2015-09-23  
引用
5 楼 whj001 2014-11-05  
很详细 ,
4 楼 594148445 2014-06-19  
老兄,请问第二种方式怎么向服务器端传递参数? 还有144行到116行代码(“OMElement symbol = fac.createOMElement("symbol", omNs); ...”)有什么作用,谢谢!
3 楼 andey007518 2014-03-27  
比较给力,收下了
2 楼 whj001 2014-03-14  
 
正需要这个。
1 楼 jveqi 2014-03-13  
先赞一下。回头看看。
看着很详细哈
3q

相关推荐

    Axis2生成webservice客户端通用依赖包

    总的来说,"Axis2生成webservice客户端通用依赖包"是一个方便开发者快速搭建和使用Web服务客户端的解决方案,它集成了必要的库,简化了项目集成过程,提高了开发效率。在实际开发中,理解和掌握这一方法将极大地提升...

    基于axis2实现的webservice简单实现(客户端+服务端)。

    【标题】中的“基于axis2实现的webservice简单实现(客户端+服务端)”表明了本文将探讨如何使用Apache Axis2框架来创建和消费Web服务。Apache Axis2是Apache软件基金会开发的一个Web服务引擎,它提供了高效且灵活的...

    axis2组建webservice,客户端必须的jar包

    Axis2是Apache软件基金会开发的一个开源Web服务框架,它提供了构建高效、可扩展的Web服务的能力。本话题将详细阐述如何使用Axis2创建Java Web服务以及客户端在调用这些服务时所需的重要依赖库。 1. **Axis2组件**:...

    axis开发webservice客户端

    标题中的“axis开发webservice客户端”指的是使用Apache Axis框架创建并使用Web服务客户端的过程。Apache Axis是Java平台上的一个开源工具,它简化了SOAP(Simple Object Access Protocol)Web服务的开发,包括...

    axis2开发webservice(二)

    资源包含了:axis2-1.7.4-bin.zip、axis2-1.7.4-war.zip、axis2-eclipse-...备注:资源超过了70M 分成了3部分 见axis2方式开发webservice(一)和 axis2方式开发webservice(二)、 axis2方式开发webservice(三)

    axis2开发webservice

    标题 "Axis2 开发 WebService" 指的是使用 Apache Axis2 框架在 Eclipse 集成开发环境中创建和部署 WebService 的过程。Apache Axis2 是一个强大的 WebService 引擎,它提供了高性能、灵活且可扩展的架构,支持多种...

    使用cxf wsdl2java生成webservice客户端

    使用cxf wsdl2java生成webservice客户端命令

    axis2方式开发webservice

    资源包含了:axis2-1.7.4-bin.zip、axis2-1.7.4-war.zip、axis2-eclipse-...备注:资源超过了70M 分成了2部分 见axis2方式开发webservice(一)和 axis2方式开发webservice(二)、 axis2方式开发webservice(三)

    axis2发布webservice和调用axis2服务接口

    在IT行业中,Axis2是Apache软件基金会开发的一个用于构建Web服务和Web服务客户端的框架,主要基于Java语言。本文将详细讲解如何使用Axis2来发布Web服务以及如何生成客户端代码来调用这些服务。 首先,让我们了解...

    axis2客户端调用webService接口,精简jar包

    总的来说,使用Axis2客户端调用WebService接口是一种常见的开发实践,通过精简jar包可以优化项目的体积,提高部署效率。理解这个过程并掌握如何精简jar包,对于任何涉及到Web服务的开发工作都是十分有益的。

    Axis2开发webservice总结.doc

    总结来说,Axis2是Apache软件基金会的一个开源项目,提供了一种高效且灵活的方式来开发和部署Web服务。开发者可以利用其强大的功能来构建复杂的分布式应用程序,实现跨平台的数据交换。理解Web服务的基本原理,熟练...

    axis2开发webservice(三)

    资源包含了:axis2-1.7.4-bin.zip、axis2-1.7.4-war.zip、axis2-eclipse-...备注:资源超过了70M 分成了3部分 见axis2方式开发webservice(一)和 axis2方式开发webservice(二)、 axis2方式开发webservice(三)

    axis2的webService服务器端和客户端开发项目

    本项目将详细介绍如何使用Axis2进行Web服务的服务器端和客户端开发。 1. **Axis2简介** Axis2是基于SOAP(简单对象访问协议)的第二代Web服务引擎,它是Apache Axis1.x的升级版。Axis2提供了一个模块化和可扩展的...

    使用axis自动生成webservice客户端代码.docx

    【使用Axis自动生成WebService客户端代码】是Web服务开发中的一个重要步骤,它简化了与服务交互的复杂性。本文档主要介绍了如何在MyEclipse 7.0环境下利用Axis 1.4工具生成客户端代码,以便调用并测试Webservice。 ...

    使用axis自动生成webservice客户端代码.pdf

    【使用Axis自动生成WebService客户端代码】是Web服务开发中的一个重要步骤,它简化了客户端调用Web服务的过程。本文档主要介绍了如何使用Axis工具生成客户端代码,以Myeclipse7.0为开发环境,axis1.4为工具版本,...

    axis2支持webservice 自动生成代码客户端服务端代码插件

    标题提及的“axis2支持webservice 自动生成代码客户端服务端代码插件”,是指Apache Axis2为Eclipse IDE提供的一套工具,旨在简化Web服务的开发流程。这个插件允许开发者通过WSDL(Web Services Description ...

    client_axis.rar_AxisClient_axis client_axis.client_webservice客户端

    "AxisClient"是Apache Axis的一部分,它为开发人员提供了一种方式来消费Web服务,通过生成Java代理类来调用远程服务的方法。 描述中的“基于axis实现的webservice客户端调用”进一步确认了这个项目的核心功能。使用...

    SpringBoot开发WebService之Axis示例

    总的来说,SpringBoot和Axis的结合为Java开发者提供了一种快速开发Web服务的方法。通过理解这两个工具的核心功能以及如何将它们集成在一起,我们可以更好地利用它们的特性来满足项目需求,同时享受到SpringBoot带来...

Global site tag (gtag.js) - Google Analytics