zoukankan      html  css  js  c++  java
  • axis1

    引用

    力宝axis1
    Axis2是一套崭新的WebService引擎,该版本是对Axis1.x重新设计的产物。Axis2不仅支持SOAP1.1和 SOAP1.2,还集成了非常流行的REST WebService,同时还支持Spring、JSON等技术。这些都将在后面的系列教程中讲解。在本文中主要介绍了如何使用Axis2开发一个不需要任何配置文件的WebService,并在客户端使用Java和C#调用这个WebService。

      一、Axis2的下载和安装

      读者可以从如下的网址下载Axis2的最新版本:

      http://ws.apache.org/axis2/

      在本文使用了目前Axis2的最新版本1.4.1。读者可以下载如下两个zip包:

      axis2-1.4.1-bin.zip

      axis2-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文件放到<Tomcat安装目录>webapps目录中(本文使用的Tomcat的版本是6.x),并启动Tomcat。

      在浏览器地址栏中输入如下的URL:

      http://localhost:8080/axis2/

      如果在浏览器中显示出如图1所示的页面,则表示Axis2安装成功。


     

     

    二、编写和发布WebService

      对于用Java实现的服务程序给人的印象就是需要进行大量的配置,不过这一点在Axis2中将被终结。在Axis2中不需要进行任何的配置,就可以直接将一个简单的POJO发布成WebService。其中POJO中所有的public方法将被发布成WebService方法。

      下面我们来实现一个简单的POJO,代码如下:

    Java代码

    1. publicclassSimpleService   
    2. {   
    3.   publicStringgetGreeting(Stringname)   
    4.   {   
    5.     return"你好"+name;   
    6.   }     
    7.   publicintgetPrice()   
    8.   {   
    9.     returnnewjava.util.Random().nextInt(1000);   
    10.   }     
    11.  

    publicclassSimpleService {   publicStringgetGreeting(Stringname)   {     return"你好"+name;   }     publicintgetPrice()   {     returnnewjava.util.Random().nextInt(1000);   }   }

       在SimpleService类中有两个方法,由于这两个方法都是public方法,因此,它们都将作为WebService方法被发布。

      编译SimpleService类后,将SimpleService.class文件放到<Tomcat安装目录>webappsaxis2WEB-INFpojo目录中(如果没有pojo目录,则建立该目录)。现在我们已经成功将 SimpleService类发布成了WebService。在浏览器地址栏中输入如下的URL:

      http://localhost:8080/axis2/services/listServices

      这时当前页面将显示所有在Axis2中发布的WebService,如图2所示。


     图2

     

      在浏览器地址栏中输入如下的两个URL来分别测试getGreeting和getPrice方法:

      http://localhost:8080/axis2/services/SimpleService/getGreeting?name=bill

      http://localhost:8080/axis2/services/SimpleService/getPrice

      图3和图4分别显示了getGreeting和getPrice方法的测试结果。
     图3 getGreeting方法的测试结果

     图4 getPrice方法的测试结果

    在编写、发布和测试0配置的WebService时应注意如下几点:

      1.POJO类不能使用package关键字声明包。

     2.Axis2在默认情况下可以热发布WebService,也就是说,将WebService的.class文件复制到pojo目录中时,Tomcat不需要重新启动就可以自动发布WebService。如果想取消Axis2的热发布功能,可以打开<Tomcat安装目录>webappsaxis2WEB-INFconfaxis2.xml,找到如下的配置代码:

    Xml代码

    1. <parameternameparametername="hotdeployment">true</parameter>  

    <parametername="hotdeployment">true</parameter>

      将true改为false即可。要注意的是,Axis2在默认情况下虽然是热发布,但并不是热更新,也就是说,一旦成功发布了 WebService,再想更新该WebService,就必须重启Tomcat。这对于开发人员调试WebService非常不方便,因此,在开发 WebService时,可以将Axis2设为热更新。在axis2.xml文件中找到<parameter name="hotupdate">false</parameter>,将false改为true即可。

      3.在浏览器中测试WebService时,如果WebService方法有参数,需要使用URL的请求参数来指定该WebService方法参数的值,请求参数名与方法参数名要一致,例如,要测试getGreeting方法,请求参数名应为name,如上面的URL所示。

      4.发布WebService的pojo目录只是默认的,如果读者想在其他的目录发布WebService,可以打开axis2.xml文件,并在<axisconfig>元素中添加如下的子元素:

    Xml代码

    1. <deployerextensiondeployerextension=".class"directory="my"class="org.apache.axis2.deployment.POJODeployer"/>  

    <deployerextension=".class"directory="my"class="org.apache.axis2.deployment.POJODeployer"/>

      上面的配置允许在<Tomcat安装目录>"webapps"axis2"WEB-INF"my目录中发布WebService。例如,将本例中的SimpleService.class复制到my目录中也可以成功发布(但要删除pojo目录中的 SimpleService.class,否则WebService会重名)。

      三、 用Java实现调用WebService的客户端程序

     WebService是为程序服务的,只在浏览器中访问WebService是没有意义的。因此,在本节使用Java实现了一个控制台程序来调用上一节发布的WebService。调用WebService的客户端代码如下:

      packageclient;

    Java代码

    1. package client;   
    2.   
    3. import javax.xml.namespace.QName;   
    4.   
    5. import org.apache.axis2.addressing.EndpointReference;   
    6. import org.apache.axis2.client.Options;   
    7. import org.apache.axis2.rpc.client.RPCServiceClient;   
    8.   
    9. public class RPCClient    
    10. {   
    11.       
    12.     public static void main(String[] args) throws Exception   
    13.     {   
    14.         invokeGetGreeting();   
    15.         invokeGetPrice();   
    16.     }   
    17.        
    18.       
    19.     public static void invokeGetGreeting() throws Exception   
    20.     {   
    21.         // 使用RPC方式调用WebService   
    22.         RPCServiceClient rpcServiceClient new RPCServiceClient();   
    23.         Options options rpcServiceClient.getOptions();   
    24.            
    25.         // 指定调用WebService的URL   
    26.         EndpointReference to new EndpointReference("http://localhost:8080/Axis2POJOServer/services/SimpleService");   
    27.         options.setTo(to);   
    28.            
    29.         // 指定getGreeting方法的参数值   
    30.         Object[] opAddEntryArgs new Object[]{"超人"};   
    31.            
    32.         // 指定getGreeting方法返回值的数据类型的Class对象   
    33.         Class[] classes new Class[]{String.class};   
    34.            
    35.         // 指定要调用的getGreeting方法及WSDL文件的命名空间   
    36.         QName opAddEntry new QName("http://myservice", "getGreeting");   
    37.            
    38.         // 调用getGreeting方法并输出该方法的返回值   
  • 相关阅读:
    页面静态化3 --- 伪静态技术
    9.14[XJOI] NOIP训练33
    9.13[XJOI] NOIP训练32
    Hello world!
    BZOJ-1853: [Scoi2010]幸运数字 (容斥原理)
    luogu1983[NOIP2013pjT4] 车站分级(拓扑排序)
    luogu1113 杂物 (拓扑排序)
    POJ-1094 Sorting It All Out && luogu1347 排序 (拓扑排序)
    BZOJ-1965: [Ahoi2005]SHUFFLE 洗牌 (快速幂+乘转加)
    BZOJ-2705: [SDOI2012]Longge的问题 (欧拉函数)
  • 原文地址:https://www.cnblogs.com/alaricblog/p/3278265.html
Copyright © 2011-2022 走看看