Axis2实现webservice实例

1.编写Axis2服务器端代码

import  java.util.Random;
/**
  * 服务器端代码就是放到Tomcat下的webapps中
  * 注意方法的输入参数和返回值类型
  *
  */
public  class  HelloAxis2Service {
 
     public  String getMessage(String name) {
 
         return  name + " say: hello Axis2" ;
     }
 
     public  String getCount( int  i) {
         return  "你是第 "  + new  Random().nextInt( 10000 ) + " 位学习者" ;
     }
}

 注意:上面的类并没有package

 拷贝这个类的class文件HelloAxis2Service.class放到tomcat目录下的webapps的axis2的WEB-INF目录的pojo文件夹下。如果没有pojo这个目录就手动创建一个一个文件夹。

  重新启动tomcat,访问项目,点击“Services”,出现如图

则证明服务器端部署成功。

点击链接就可以看到wsdl内容了,内容很多,但主要注意绿线标注的内容

那为什么要将class文件放到pojo文件夹下呢?打开apache-tomcat-6.0.24\webapps\axis2\WEB-INF\conf下的xml文件,大家会看到这样一段内容

    <!--POJO deployer , this will alow users to drop .class file and make that into a service-->
    <deployer extension=".class" directory="pojo" class="org.apache.axis2.deployment.POJODeployer"/>
    <deployer extension=".jar" directory="servicejars"
              class="org.apache.axis2.jaxws.framework.JAXWSDeployer"/>
    <deployer extension=".jar" directory="transports"
              class="org.apache.axis2.deployment.TransportDeployer"/>

   2.编写客户端代码

    客户端需要将下载到的Binary Distributionlib包中的jar加入到当前工程或者另建的工程中。

    客户端代码如下

import  javax.xml.namespace.QName;
import  org.apache.axis2.AxisFault;
import  org.apache.axis2.addressing.EndpointReference;
import  org.apache.axis2.client.Options;
import  org.apache.axis2.rpc.client.RPCServiceClient;
 
public  class  HelloAxis2Client {
 
     public  static  void  main(String args[]) {
         try  {
             // RPC方式调用
             RPCServiceClient client = new  RPCServiceClient();
             Options options = client.getOptions(); // 注意:不是new Options()
             // 要调用的webService的url
             String serviceERP = "http://localhost:8080/axis2/services/HelloAxis2Service" ;// 地址栏内容去掉?wsdl
             EndpointReference reference = new  EndpointReference(serviceERP);
             options.setTo(reference);
             String targetNamespace = "http://ws.apache.org/axis2" ;// 绿线标注的内容
             // 1.设置要调用的方法 即sayHello。如果服务端没有提供相应的方法,当然就会报错了
             // 2.默认命名空间下没有package,即是http://ws.apache.org/axis2
             // 3.如果有package,要调用方法所在的包名倒过来写即可
             // 4.比如sayHello 所在包名是com.service 命名空间就是http://service.com
             QName qname = new  QName(targetNamespace, "getMessage" );
 
             Object[] result = client.invokeBlocking(qname,
                     new  Object[] { "wangchenyang"  },
                     new  Class[] { String. class  });
             System.out.println(result[ 0 ]);
         } catch  (AxisFault e) {
             e.printStackTrace();
         }
     }
}

 输出结果:wangchenyang say: hello Axis2。

3.总结

看到hello axis2心里就踏实了许多,怎么说也是入门了。

下面有几点需要注意

   1.使用pojo这种形式有局限性,提供服务的类不能放到包中,如果类多的话当然就不方便了 

   2.QName是个什么东东呢,网上有好多,为了让大家尽快掌握就随便拷了一份

    官方解释:

[Definition:]   QName represents XML qualified names. The ·value space· of QName is the set of tuples { namespace name, local part}, where  namespace name is an anyURI and  local part is an NCName. The ·lexical space· of QName is the set of strings that ·match· the QName production of [Namespaces in XML]. 

            也就是说QName是由一个命名空间名称前缀和NCName(就是XML元素的名称)csdn上的一个blog的解释:

Qname这个东东,在使用dom4j的时候,经常见到,可能是自己解析的xml都太简单了,所以没有仔细研究过,就是觉得名字很怪异,在google百度搜索“什么是qname”,居然只有几条没有像样答案的纪录。还好,在国外网站上找到了相关的解释,这下基本明白了。
1.来历:qname是qualified name 的简写
2.构成:由名字空间(namespace)前缀(prefix)以及冒号(:),还有一个元素名称构成
3.举例:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
     version="1.0">
  <xsl:template match="foo">
    <hr/>
  </xsl:template>
</xsl:stylesheet>
xsl是名字空间前缀,template是元素名称,xsl:template 就是一个qname 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值