axis2 webService开发(打包aar文件 )

首先感谢 http://deltaj.iteye.com/blog/287067  的博主:deltaj   
        本文主要是根据他的文章和自己实际开发中遇到的一些问题得出的,你也可以查看原文

相关文章:

axis web服务(axis2安装和eclipse、tomcat结合开发)

axis2 webService开发

axis2 webService开发(打包aar文件)


         安装好axis容器、eclipse axis2插件后就可以用打包aar文件方式发布web service服务了

服务端:

我们的服务很简单的,就是输入一个字符串,然后打印出一段字符串。

代码很简单,主要是测试用(思想是一样的)。

 

Java代码  复制代码   收藏代码
  1. package com.deltaj.server;   
  2.   
  3. public class SimpleServer {   
  4.   
  5.     /**  
  6.      * 简单的测试方法  
  7.      *   
  8.      */  
  9.     public String simpleMethod(String name) {   
  10.         return name + "Say this is a Simple method ^-^";   
  11.     }   
  12.   
  13. }  

 

主要的过程就是如何利用axis2的eclispe插件来发布这个服务啦。

 

1。在eclispe 的package Explorer 中点击右键,在菜单中选择新建--->other...----->Axis2 Service Archiver

 

2.然后点击next进入了类选择页面,在这个页面中的Class File Location选择框中选择类所在的文件夹。 


 3.点击next之后进入了选择 wsdl文件,这里我们选择skip wsdl。

 

 

4. 点击next之后,进入的是选择jar文件的页面,这里我们没有外部的jar,所以点击next直接跳过这个页面。


 4.点击next之后,进入的是选择xml页面,这里我们选择的是自动生成xml,也就是勾选

Generate the service xml automatically这一项


 5.点击next之后,进入的是生成xml文件的页面,在service name 里填写这个服务所起的名字,这里我起名为simpleServer,然后在class name 中填写要发布的类,这里一定要写全路径,写好后就可以点击load 按钮,

如果一切ok的话,你会看到如下画面


 6 点击next 后,进入的是输出artiver文件的页面,先要在output File location 中选择要输出的路径,

   在output  File Name中输入artiver文件的名称。我起的名字是simpleServer(这一步中有的会出现不发布不成功,主要是版本问题,只要把Eclipse axis2插件换成最新的就可以了;我用过1.4.1的就会报错,建议是用更高的版本)


 7.点击finish ,如果看到如下的画面,恭喜你,服务发布成功啦。

 

8.接下来,我们就可以把这个aar文件放入tomcat中发布,首先把生成的aar文件拷贝到tomcat目录中的axis2项目的service目录中位置如图。



 

9.接下来启动tomcat,在地址栏中输入http://localhost:8080/axis2 ,你会看到axis2的欢迎画面



 

 

10.点击Service连接,你会看到发布的服务列表。这里面就能看到我们发布的simpleService


 11.点击我们的服务simpleServer的连接,我们会看到。至此,服务发布成功。

客服端:

这节我们就来写一个客户端来调用一下这个服务。主要关注一下如何用elispe的axis2的插件来生成stub代码。

1.在eclispe 的package Explorer 中点击右键,在菜单中选择新建--->other...----->Axis2 Code Generator




 

2.点击next,进入下一个页面,选择从wsdl文件来产生java文件。

 

3. 点击next,然后选择wsdl文件,注意此处要填写上一节我们

 4.点击next,进入设置页面,这里我们就用默认的设置。

 

5. 点击next,选择输出文件的路径。

 

6.点击next,如果看到这个页面,恭喜你已经生成代码成功。

 

7.在package Explorer中刷新一下项目,然后你发现出现2个新的文件SimpleServerStub和SimpleServerCallbackHandler 。打开SimpleServerStub你会惊喜的发现。著名的小红叉一个接一个的

这是因为没有axis2的类包。我们可以在下载的axis2-1.4.1-bin中找到lib包,把其中的jar都加入我们的工程中。

然后重新编译一下工程,这时我们发现SimpleServerStub还是有几个小红叉。这个是因为这个插件有个小bug。

生成的代码没有实现序列化方法。我们可以自己来加上,在小红叉上点一下,弹出一个小菜单,选择

Add unimplemented methods .



 

8.

Java代码 复制代码   收藏代码
  1. /**  
  2.  * 调用发布的服务。  
  3.  *   
  4.  */  
  5. public class SimpleClient {   
  6.     
  7.  public static void main(String[] args) throws Exception{   
  8.      
  9.   //初始化桩文件   
  10.   SimpleServerStub stub = new SimpleServerStub();   
  11.   //初始化SimpleMethod方法。  
  12.   SimpleServerStub.SimpleMethod request = new  SimpleServerStub.SimpleMethod();   
  13.   //调用simpleMethod的setName方法。  
  14.   request.setName("zt");   
  15.   //   
  16.   System.out.println(stub.simpleMethod(request).get_return());   
  17.      
  18.      
  19.  }   
  20. }  
/**
 * 调用发布的服务。
 * 
 */
public class SimpleClient {
 
 public static void main(String[] args) throws Exception{
  
  //初始化桩文件
  SimpleServerStub stub = new SimpleServerStub();
  //初始化SimpleMethod方法。
  SimpleServerStub.SimpleMethod request = new  SimpleServerStub.SimpleMethod();
  //调用simpleMethod的setName方法。
  request.setName("zt");
  //
  System.out.println(stub.simpleMethod(request).get_return());
  
  
 }
}

   如果一切正常,你就会看到结果

log4j:WARN No appenders could be found for logger (org.apache.axis2.description.AxisService).
log4j:WARN Please initialize the log4j system properly.
ztSay this is a Simple method ^-^。

调用服务成功。这是个简单的例子


 

相关文章:

axis web服务(axis2安装和eclipse、tomcat结合开发)

axis2 webService开发

axis2 webService开发(打包aar文件)


你可能感兴趣的:(webservice)