微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Axis2开发WebService服务端及客户端

分类: 系统运维

参考  http://www.ibm.com/developerworks/webservices/library/ws-apacheaxis/index.html

下载axis2-1.5.4-bin.zip文件并解压。设置axis2的环境变量 " AXIS2_HOME"

服务端

We will generate the service components for the web service. For this we will use the standard Axis2's WSDL2Java command. We will use Axis2 Data binding Framework (ADB) as the data binding framework (you are free to choose any data binding framework which Axis2 supports). For generating the server-side components of the web service,execute the following command in the same command prompt window. 

利用 wsdl 文件生成服务端代码

  1. %AXIS2_HOME%\bin\WSDL2Java -uri StockQuoteService.wsdl -d adb -s -ss -sd -ssi -o service

WSDL2Java命令参数说明: 
  -uri  指定*.wsdl文件,可以带具体路径;  
  -p  指定生成代码的包名 
  -d  使用不同的数据绑定方法;  
adb , xmlbeans  

  -o  指定生成代码放置的路径; 
  -ss 表示要生成服务端代码;  
  -ssi 表示要生成代码中,先生成接口类,再生成实现类;
 


新建web工程(服务端)  结构如下

  • WebContent
  •   |-- WEB-INF
  •        |-- classes    
  •        |-- lib                        添加 axis2 的jar包
  •        |-- services        固定目录名称   将生成resources目录下的文件放入 
  •        |     |-- StockQuoteService    目录名称可随意取
  •        |           |-- meta-inf
  •        |                 |-- services.xml      固定名称 
  •        |                 |-- StockQuoteService.wsdl   用于发布的wsdl文件
  •        |-- web.xml
  •        |-- weblogic.xml


  • 编辑web.xml加入如下代码: 

  •   <servlet>
        <servlet-name>AxisServlet</servlet-name>
        <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>AxisServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
      </servlet-mapping>
  • weblogic.xml 适用于weblogic 服务器
      解决 java.lang.NoSuchMethodError: javax.xml.namespace.QName.<init>(Ljava/lang/String;
    Ljava/lang/String;Ljava/lang/String;)V 问题

    <?xml version="1.0" encoding"UTF-8"?>
  • !DOCTYPE weblogic-web-app PUBLIC "-//BEA Systems,Inc.//DTD Web Application 8.1//EN" "http://www.bea.com/servers/wls810/dtd/weblogic810-web-jar.dtd">

  • <weblogic-web-app>

  •   <container-descriptor>
  •     <prefer-web-inf-classes>true</prefer-web-inf-classes>
  •   /container-descriptor<context-root>StockQuoteWS/context-root>
  • /weblogic-web-app>
  • 打包发布  http://localhost:8080/StockQuoteWS/services/StockQuoteService?wsdl


    客户端  调用三种方法  前两种不生成客户端代码  但不太好用 

    点击(此处)折叠或打开

      package samples.quickstart.client;

    1. import javax.xmlnamespaceQName;
    2. org.apache.axiom.om.OMAbstractFactory.OMElement.OMFactory.OMNamespace.axis2.AxisFault.addressing.EndpointReference.Options.ServiceClient.rpc.RPCServiceClientimport samples.StockQuoteServiceStub.xsd.GetPrice.GetPriceResponse;

    3. public class StockQuoteClient {

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

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

    31.       // 使用RPC方式调用WebService
    32.       RPCServiceClient serviceClient = new RPCServiceClient);
    33.       // 指定调用WebService的URL
    34.       EndpointReference targetEPR new EndpointReference(url;
    35.       Options options = serviceClientgetoptions//确定目标服务地址
    36.       options.setTo(targetEPR//确定调用方法
    37.       optionssetAction"urn:getPrice"/**
    38.        * 指定要调用的getPrice方法及WSDL文件的命名空间 
    39.        * 如果 webservice 服务端由axis2编写 
    40.        * 命名空间 不一致导致的问题
    41.        * org.apache.axis2.AxisFault: java.lang.RuntimeException: Unexpected subelement arg0 
    42.        */
    43.       QName qname new "http://quickstart.samples/xsd", "getPrice"// 指定getPrice方法的参数值
    44.       Object[] parameters ] { "13" };
    45.       
    46.       // 指定getPrice方法返回值的数据类型的Class对象
    47.       Class] returnTypes new { double.class // 调用方法一 传递参数,调用服务,获取服务返回结果集
    48.       OMElement element .invokeBlockingqnamearameters//值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。
    49.       //我们可以对之灵活应用,下面我取第一个元素值,并打印之。因为调用方法返回一个结果
    50.       result element.getFirstElementgetTextSystem.outprintlnresult// 调用方法二 getPrice方法输出方法的返回值
    51.       ] response // String r = (String) response[0];
    52.       Double r Double) response[0](r;

    53.     } catch (AxisFault e{
    54.       eprintstacktrace;
    55.     }
    56.   }

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

    63.       Options options new Options;
    64.       options// options.setAction("urn:getPrice");

    65.       ServiceClient sender new ServiceClient;
    66.       sendersetoptions(options;
    67.       
    68.       
    69.       OMFactory fac = OMAbstractFactory.getoMFactoryString tns "http://quickstart.samples/"// 命名空间,有时命名空间不增加没事,不过最好加上,因为有时有事,你懂的
    70.       OMNamespace omNs = fac.createOMNamespace(tns"";

    71.       OMElement method .createOMElement;
    72.       OMElement symbol "symbol"// symbol.setText("1");
    73.       symbol.addChild(fac.createOMText(symbol"Axis2 Echo String "methodbuild;
    74.       
    75.       OMElement = sender.sendReceive(AxisFault axisFault{
    76.       axisFault}

    77.  /**
    78.   * 为SOAP Header构造验证信息, 
    79.   * 如果你的服务端是没有验证的,那么你不用在Header中增加验证信息
    80.   * 
    81.   * @param serviceClient
    82.   * @param tns 命名空间 
    83.   * @param user 
    84.   * @param passwrod
    85.   */
    86.   void addValidation(ServiceClient serviceClientString userString passwrod{
    87.     OMFactory fac ;
    88.     OMNamespace omNs "nsl";
    89.     OMElement header "AuthenticationToken";
    90.     OMElement ome_user "Username";
    91.     OMElement ome_pass "Password";
    92.     
    93.     ome_usersetText(user;
    94.     ome_pass(passwrod;
    95.     
    96.     header(ome_user;
    97.     header(ome_pass;

    98.     serviceClient.addHeader(header;
    99.   }

    100.   
    101.   /**
    102.    * 方法三:利用axis2插件生成客户端方式调用
    103.    * 
    104.    */
    105.   void testCodeClient"http://localhost:8080/axis2ServerDemo/services/StockQuoteService";
    106.       StockQuoteServiceStub stub new StockQuoteServiceStub;
    107.       GetPrice request new GetPricerequest.setSymbol"ABCD";
    108.       GetPriceResponse response stub.getPrice(response.get_return.AxisFault ejavarmiremoteexception e}

    109.   void mainString] args{
    110.      StockQuoteClient.testRPcclient;
    111. // StockQuoteClient.testDocument();
    112.     // StockQuoteClient.testCodeClient();

    113.   }
    114. }





    利用 axis2 的wsdl2java.bat生成客户端程序

    %AXIS2_HOME%\bin\ wsdl2java  -uri StockQuoteService.wsdl  -d adb  -p axis2.service.server  -o  client  -u
       其中-uri 参数指定了wsdl 文件的路径,可以是本地路径,也可以是网络路径。-p 参数指定了生成Java 类的包名,-o 参数指定了生成的一系列文件保存的根目录。加上可选参数  -u Unpacks the databinding classes  避免生成的ServiceStub类特别大 在执行完上面的命令后,读者就会发现在当前目录下多了个 client 目录, 可以找到一个HelloServiceStub.java 文件,该文件复杂调用WebService ,读者可以在程序中直接使用这个类。

    其中HelloServiceStub.java是客户端程序,HelloServiceCallbackHandler.java是异步客户端程序。


    Axis2 客户端最少依赖包

  • activation-1.1.jar
  • axiom-api-1.2.4.jar
  • axiom-impl-1.2.4.jar
  • axis2-adb-1.2.jar
  • axis2-kernel-1.2.jar
  • commons-codec-1.3.jar
  • commons-httpclient-3.0.1.jar
  • commons-logging-1.1.jar
  • stax-api-1.0.1.jar
  • wsdl4j-1.6.2.jar
  • wstx-asl-3.2.1.jar
  • XmlSchema-1.3.1.jar
  • 版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

    相关推荐