axis2开发实例(一)

axis2开发实例

 

主要参考《axis2之webservice新手超详细教程http://wenku.baidu.com/view/6eae036d011ca300a6c390a4.html

        《axis2教程http://wenku.baidu.com/link?url=jBDwQFFG2yipTU_bRpre6hylOrakAJ4j-V3N8Wt4eTVdXkwunCM7-JIyluaCOAAHgBX3N52HRjYzSoXG6aBs5-E-RxPCDPEFJf6YFzXe3gq

         myeclipse的axis2插件:《Axis2_Codegen_Wizard_1.3.0 和Axis2_Service_Archiver_1.3.0》

         地址:http://download.csdn.net/download/huahuaspy/3428267

 

 

 

 

 

 

 

 

第一部分 环境搭建

1. 环境搭建(jdk,tomcat,myeclipse已安装前提下)

(1)       下载Axis2服务包:axis2-1.6.2-bin.zip,axis2-1.6.2-war.zip,分别解压到D:\webservice_axis2,将axis2-1.6.2-war.zip解压后的axis2.war放到tomcat的D:\tomcat-6.0.18\tomcat-6.0.18\webapps下,重启tomcat,

访问http://localhost:8080/axis2/,能看到页面内容,如果不能访问,查看tomcat端口是否8080

(2)       下载myeclipse的axis2插件Axis2_Codegen_Wizard_1.3.0 和Axis2_Service_Archiver_1.3.0,一个是帮助我们生成aar文件的,另一个是帮我们用wsdl文件生成stub代码的。解压后将axis2插件包下的Axis2_Codegen_Wizard_1.3.0和Axis2_Service_Archiver_1.3.0文件夹拷贝至Myeclipse8.5\dropins 目录下,重启myeclipse生效

 

2.代码开发:本实例练习基本参数、bean,数组bean

作为入参和回参的情况,能满足大部分项目需求

 

(1)     新建web工程Axis2Service,创建入参回参bean(注意红色部分)

Student.java

package com.my.bean;

import java.io.Serializable;

/**

 * 注意:Student类一定要实现序列化接口,否则无法正常传输至客户端

 *

 * @author zyix

 *

 */

public class Student implements Serializable

{

    private static final long serialVersionUID = 677484458789332877L;

 

    private int id;

    private int number;

    private String name;

    private String tel;

    private String address;

    //生成setter,getter省略

    }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(3)       创建service,用于发布

MyService.java

     

package com.my.service;

import java.util.Arrays;

import com.my.bean.Student;

/*

 * WebService测试类

 */

public class MyService

{

        

         // 测试欢迎方法,基本参数

         public String getGreeting(String name)

         {

                   return "你好" + name;

         }

 

         // 入参为bean

         public String AddStudent(Student student)

         {

                   if (null != student)

                   {

                            return "你好" + student.getName();

                   }

                   return "student is null";

         }

        

         // 回参为bean

         public Student queryStudent()

         {

                   Student st = new Student();

                   st.setId(1000000120);

                   st.setNumber(20130678);

                   st.setName("Mr Wang");

                   st.setTel("1385465656");

                   st.setAddress("bejing");

                   return st;

         }

        

         // 入参为数组

         public String AddStudents(Student[] students)

         {

                   if (null != students)

                   {

                            return "你好" + Arrays.toString(students);

                   }

                   return "students is null";

         }

        

         // 回参为数组

         public Student[] queryStudents()

         {

                   Student[] sts = new Student[2];

                   Student st = new Student();

                   st.setId(1000000120);

                   st.setNumber(20130678);

                   st.setName("Mr Wang");

                   st.setTel("1385465656");

                   st.setAddress("bejing");

                  

                   Student st2 = new Student();

                   st2.setId(1000000122);

                   st2.setNumber(201306782);

                   st2.setName("Mr Wang2");

                   st2.setTel("1385465652");

                   st2.setAddress("bejing2");

                  

                   sts[0] = st;

                   sts[1] = st2;

                   return sts;

         }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3.发布service

(1)右键工程名Axis2Service--NewàOther,选择Axis2 Service Archiver,

(2)打包编译文件,指向编译好的service文件位置,一般都是XXX\WebRoot\WEB-INF\classes,选择E:\workspaces_8.5\Axis2Service\WebRoot\WEB-INF\classes目录,记得红色标记处的勾选取消掉

Ps:这里指定位置都是WEB-INF\classes目录,因为第(4)步填写的service包路径就是相对于此目录的

(3)下一步略过wsdl文件选择,下一步选择依赖的的jar文件,没有可不用选择,下一步自动设置service.xml配置文件,

(4)下一步,填写需要发布的webservice名称以及对外开放的服务类名称(自定义),服务类名称前面要带包名,(需要手动填写service路径,点击Load)

勾选Search declared method only,可以在下面的列表框中看见我们对外发布的方法名

(5)下一步,指定发布service到tomcat的axis2项目容器中,这里选择D:\tomcat-6.0.18\tomcat-6.0.18\webapps\axis2\WEB-INF\services;

Ps:本质就是将自动打成的my_service.aar放到上述路径;其实可以先指定其他路径,生成后,再手动放置需要的容器中。

(6)启动tomcat,访问http://localhost:8080/axis2/services/listServices,即可看到新发布的service:MyService,点击MyService链接,得到wsdl文件。

至此发布完成。

 

第二部分  使用客户端测试webservice

(1)       新建web工程Axis2Client,引入axis2的lib包,如D:\webservice_axis2\axis2-1.6.2-bin\axis2-1.6.2\lib

(2)       根据wsdl生成客户端,有两种方式

方式一:使用myeclipse的axis2插件Axis2 Code Generator生成客户端

方式二:使用基本的命令行方式

Ps:本人使用方式一生成的代码不能够编译,提示student类有问题;因此采用方式二:

(3)方式二:制作《根据wsdl生成java代码.bat》

1.在D:\webservice_axis2中,已有axis2-1.6.2-bin,目标wsdl文件如Axis2Wsdl.wsdl

新建根据wsdl生成java代码.bat,内容如下:

set JAVA_HOME=D:\Program Files\Java\jdk1.6.0_10

set AXIS2_HOME=D:\webservice_axis2\axis2-1.6.2-bin\axis2-1.6.2

 

set Output_Path=Testclient

 

set Package=com.webservice.client

 

set Wsdl_File=D:\webservice_axis2\Axis2Wsdl.wsdl

 

%AXIS2_HOME%\bin\wsdl2java -uri %Wsdl_File% -p %Package% -s -o %Output_Path%

::一定要设置JAVA_HOME

::其中-url参数指定了wsdl文件的路径,可以是本地路径,也可以是网络路径。如 http://localhost:8080/axis2/services/SimpleService?wsdl

::-p参数指定了生成的Java类的包名

::-o参数指定了生成的一系列文件保存的根目录

@echo  -------------OK-----------------

 

@echo ---The code has been created in the folder: Testclient---

 

@pause

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

绿色部分都是根据实际设置,执行批处理脚本生成的客户端代码会在Testclient中;

 

(3)       将Testclient中的src下的文件覆盖到项目Axis2Client的src下;新建测试代码

Test.java

package com.client.test;

 

import com.webservice.client.MyServiceStub;

import com.webservice.client.MyServiceStub.AddStudent;

import com.webservice.client.MyServiceStub.AddStudentResponse;

import com.webservice.client.MyServiceStub.AddStudents;

import com.webservice.client.MyServiceStub.AddStudentsResponse;

import com.webservice.client.MyServiceStub.GetGreeting;

import com.webservice.client.MyServiceStub.GetGreetingResponse;

import com.webservice.client.MyServiceStub.QueryStudent;

import com.webservice.client.MyServiceStub.QueryStudentResponse;

import com.webservice.client.MyServiceStub.QueryStudents;

import com.webservice.client.MyServiceStub.QueryStudentsResponse;

import com.webservice.client.MyServiceStub.Student;

 

public class Test

{

         public static void main(String[] ss)

         {

                   // 设置远程服务调用地址

                   String target = "http://127.0.0.1:8080/axis2/services/MyService";

 

                   try

                   {

                            // 根据地址构造用户存根

                            MyServiceStub stub = new MyServiceStub(target);

 

                            // 1. 调用getGreeting方法,设置入参对象及属性,入参,回参为基本类型String

                            GetGreeting getGreeting = new GetGreeting();

                            getGreeting.setName("hello");

                            GetGreetingResponse ggr = stub.getGreeting(getGreeting);

                            // 接收方法返回值

                            String getResult = ggr.get_return();

                            System.out.println(getResult);

 

                            // 2. 调用AddStudent方法,设置入参对象及属性,入参为bean

                            AddStudent addStudent = new AddStudent();

                            Student student = new Student();

                            student.setAddress("shanghai");

                            student.setId(123456);

                            student.setName("servyou");

                            student.setNumber(12321312);

                            student.setTel("13678956529");

                            addStudent.setStudent(student);

                            AddStudentResponse asr = stub.addStudent(addStudent);

                            // 接收方法返回值

                            String addResult = asr.get_return();

                            System.out.println(addResult);

 

                            // 3. 调用queryStudent方法,设置入参对象及属性,回参为bean

                            QueryStudent queryStudent = new QueryStudent();

                            QueryStudentResponse qsr = stub.queryStudent(queryStudent);

                            // 接收方法返回值

                            Student qsResult = qsr.get_return();

                            System.out.println("地址:" + qsResult.getAddress() + ", ID:"

                                               + qsResult.getId() + ", 姓名:" + qsResult.getName() + ", 号码:"

                                               + qsResult.getNumber() + ", 电话:" + qsResult.getTel());

 

                            // 4. 调用queryStudents方法,设置入参对象及属性,返回bean数组

                            QueryStudents queryStudents = new QueryStudents();

                            QueryStudentsResponse qssr = stub.queryStudents(queryStudents);

                            // 接收方法返回值

                            Student[] qssResult = qssr.get_return();

                            for (int i = 0; i < qssResult.length; i++)

                            {

                                     System.out.println("地址:" + qssResult[i].getAddress() + ", ID:"

                                                        + qssResult[i].getId() + ", 姓名:"

                                                        + qssResult[i].getName() + ", 号码:"

                                                        + qssResult[i].getNumber() + ", 电话:"

                                                        + qssResult[i].getTel());

                            }

 

                            // 5. 调用AddStudents方法,设置入参对象及属性,入参为bean数组

                            AddStudents addStudents = new AddStudents();

                           

                            Student st = new Student();

                            st.setAddress("shanghai");

                            st.setId(123456);

                            st.setName("servyou");

                            st.setNumber(12321312);

                            st.setTel("13678956529");

                           

                            Student st2 = new Student();

                            st2.setAddress("shanghai");

                            st2.setId(123456);

                            st2.setName("servyou");

                            st2.setNumber(12321312);

                            st2.setTel("13678956529");

                           

                            addStudents.addStudents(st);

                            addStudents.addStudents(st2);

                           

                            AddStudentsResponse assr = stub.addStudents(addStudents);

                            // 接收方法返回值

                            String addstsResult = assr.get_return();

                            System.out.println(addstsResult);

 

                   } catch (Exception e)

                   {

                            e.printStackTrace();

                   }

         }

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

第三部分 备注信息

备注:发布可以不生成arr文件,好处在于:经常改动代码或者添加安全控制模块如wss4j,因为经常改动 services.xml,所以这种情况最好不打包;否则最好打包发布

方法:

(1)    在/WEB-INF/services新建HelloWorldService(最好命名跟service命名一样,便于识别);

(2)    在HelloWorldService新建META-INF目录,在META-INF中建立services.xml文件

<service name="HelloWordService">

      <description>

           Please Type your service description here

      </description>

      <messageReceivers>

           <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>

           <messageReceiver  mep="http://www.w3.org/2004/08/wsdl/in-out"  class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>

      </messageReceivers>

      <parameter name="ServiceClass">com.service.HelloWorldService</parameter>

</service>

 

至此测试完毕

 

 

第四部分 补充

 

(1)    项目中仅需要使用WebService来传递简单类型的数据,有时也需要传递更复杂的数据,这些数据可以被称为复合类型的数据。

(2)    数组和类(接口)是比较常用的复合类型;在传递类的对象实例时,除了直接将数组类型声明成相应的类戒接口,也可以将对象实例迚行序列化,也就是说,将一个对象实例转换成字节数组迚行传递,然后接收方再迚行反序列化,还原这个对象实例。

(3)       webservice不支持复杂的集合类传递,譬如list和map,需要转化为数组类型或者axis2里的axiom对象模型。axiom对象模型感兴趣的同学可搜索资料进一步学习

 

 

 

 

 

 

 

        实际项目中可以支持webservice服务,也可以支持其他服务如http,axis1.4的webservice等,由于Axis2需要发布到axis2工程,因此实际开发时,可以在自己的web工程开发,只有需要提供webservice服务时,才做成aar包放到axis2项目中。

posted @ 2013-10-31 20:14  嗨,你的益达~~~  阅读(4428)  评论(1编辑  收藏  举报