axis2 1.7.1使用教程

写在前面

 本文只说Axis2的用法。

1、下载与部署

  需要下载两个文件:

  下载地址:http://mirrors.cnnic.cn/apache/axis/axis2/java/core/1.7.1/axis2-1.7.1-war.zip,该文件是部署所用;

      下载地址:http://mirrors.cnnic.cn/apache/axis/axis2/java/core/1.7.1/axis2-1.7.1-bin.zip,该文件是开发所用;

      解压axis2-1.7.1-war.zip会得到一个axis2.war,将之扔到tomcat之webapps目录之下,启动tomcat,war包会自动解压;

      测试:http://localhost:8080/axis2,看到欢迎界面了吧,恭喜第一步成功了!

2、配置axis2

   打开/axis2/WEB-INF/conf/axis2.xml配置文件,找到<parameter name="hotupdate">false</parameter>改为true;

     只是为了热更新,方便调试。不配置也可以的。

3、HelloWorld

    在/axis2/WEB-INF目录下,新建一个目录pojo,在该目录下新建一个java文件:

1
2
3
4
5
6
public class HelloWorld
{
    public String sayHello(){
        return "hello world!";
    }
}

   编译之,shift,在此处打开命令行,javac HelloWorld.java, 不用解释吧?

   看到多了一个HelloWorld.class文件吧,就是它!

   打开浏览器,输入:http://localhost:8080/axis2/services/HelloWorld;

4、pojo目录说明

   为什么是pojo目录,新建个不行么?

   当然行,不过需要配置一下下,打开/axis2/WEB-INF/conf/axis2.xml配置文件,找到如下内容,新增目录

1
2
<deployer extension=".class" directory="pojo" class="org.apache.axis2.deployment.POJODeployer"/><BR> <!--新增的目录-->
<deployer extension=".class" directory="pojo2" class="org.apache.axis2.deployment.POJODeployer"/>

   注意:这里面放置的类不能保护package,另外如果多个目录,service名称不能重复.

5、no package?那怎么能行!

    打开eclipse,建一个java project,并建立com.ws.test.services包,在该包下创建一个类HelloService,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.ws.test.services;
  
public class HelloService {
      
    /**
     * 没有返回值
     * @param info
     */
    public void send(String info) {
        System.out.println(info);
    }
  
    /**
     * 有返回值
     * @param x
     * @param y
     * @return
     */
    public int add(int x, int y) {
        return x + y;
    }
}

     在src目录下新建META-INF文件夹,在其下建立两个文件:MANIFEST.MF和services.xml

serviecs.xml代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
  
<service name="HelloService">
    <description>一个简单的WebService</description>
    <!-- 服务全类名 -->
    <parameter name="ServiceClass">com.ws.test.services.HelloService</parameter>
  
    <operation name="send">
        <!-- 信息接收器, 无返回值用:RPCInOnlyMessageReceiver-->
        <messageReceiver mep="http://www.w3.org/ns/wsdl/in-only"
            class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
    </operation>
    <operation name="add">
        <!-- 信息接收器, 有返回值用:RPCMessageReceiver-->
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
            class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </operation>
  
</service>

 注:axis2-1.7.1samplespojosrcMETA-INF目录下有services.xml例子,可以复制过来修改。

6、打包

     选择包名和META-INF,export为server.jar,修改为servier.aar,扔进webappsaxis2WEB-INFservices目录下,重启tomcat

     测试:http://localhost:8080/axis2,点击services,出现如下界面:

     

     成功了一大步!

   测试:http://localhost:8080/axis2/services/HelloService/send?info=hello,如果tomcat控制台输出hello,测试成功!

     测试:http://localhost:8080/axis2/services/HelloService/add?x=1&y=4,如果返回5,测试成功!

7、如何发布多个Service呢?

    在com.ws.test.services目录下新建HelloService2,代码如下:

1
2
3
4
5
6
7
package com.ws.test.services;
  
public class HelloService2 {
    public int sub(int x, int y) {
        return x - y;
    }
}

  修改services.xml文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
    <service name="HelloService">
        <description>一个简单的WebService</description>
        <!-- 服务全类名 -->
        <parameter name="ServiceClass">com.ws.test.services.HelloService
        </parameter>
  
        <operation name="send">
            <!-- 信息接收器, 无返回值用:RPCInOnlyMessageReceiver -->
            <messageReceiver mep="http://www.w3.org/ns/wsdl/in-only"
                class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
        </operation>
        <operation name="add">
            <!-- 信息接收器, 有返回值用:RPCInOnlyMessageReceiver -->
            <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
                class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
        </operation>
  
    </service>
  
    <service name="HelloService2">
        <description>一个简单的WebService 2</description>
        <parameter name="ServiceClass">com.ws.test.services.HelloService2
        </parameter>
        <operation name="sub">
            <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
                class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
        </operation>
    </service>
  
</serviceGroup>

 重新打包,扔进webappsaxis2WEB-INFservices目录下;

 测试:http://localhost:8080/axis2/services/HelloService2/sub?x=5&y=2,返回3,则测试成功!

8、如何在程序中调用webservice呢?

     先加入jar包,jar包路径:axis2-1.7.1lib*.jar

(1)直接看代码吧,这个方法比较麻烦

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.ws.test.client;
  
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 TestClient {
  
    public static void main(String[] args) throws AxisFault {
  
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
  
        EndpointReference targetEPR = new EndpointReference(
                "http://localhost:8080/axis2/services/HelloService");
        options.setTo(targetEPR);
  
        Object[] entryArgs = new Object[] { 4, 2 };
        //命名空间:wsdl:definitions标签targetNamespace属性
        QName opName = new QName("http://services.test.ws.com", "add");
  
        //有返回值
        Object result = serviceClient.invokeBlocking(opName, entryArgs,
                new Class[] { int.class })[0];
        System.out.println(result);
  
        opName = new QName("http://services.test.ws.com", "send");
  
        //无返回值
        serviceClient.invokeRobust(opName, new Object[]{"hello world!"});
          
    }
  
}

 (2)使用wsdl2java

使用方法:

①先配置axis2客户端环境变量

  AXIS2_HOME:D:Program Filesaxis2-1.7.1

  path追加:%AXIS2_HOME%in

②在项目目录下,即跟src目录同级,shift进入命令行窗口,输入如下命令:

1
wsdl2java -uri http://localhost:8080/axis2/services/HelloService?wsdl -p com.ws.test.stubs -s

③进入项目F5

是不是多了一个包叫做:com.ws.test.stubs下面有给类叫做HelloServiceStub,打开看看,吓死宝宝了,好多内容,还好不用管他。

④客户端代码,直接看代码了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.ws.test.client;
  
import java.rmi.RemoteException;
import com.ws.test.stubs.HelloServiceStub;
import com.ws.test.stubs.HelloServiceStub.Add;
  
public class TestClient2 {
  
    public static void main(String[] args) throws RemoteException {
        HelloServiceStub stub = new HelloServiceStub();
        Add add = new Add();
        add.setX(1);
        add.setY(2);
        int result = stub.add(add).get_return();
        System.out.println(result);
    }
}

 宝宝用起来很舒服!

9、wsdl2java好强大,是不是可以转换任何的WebService啊?

      当然可以了,废话少说,代码如下:

①干啥?当然运行命令了

1
wsdl2java -uri http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl -p com.ws.test.stubs -s

②客户端测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.ws.test.client;
  
import java.rmi.RemoteException;
import com.ws.test.stubs.WeatherWebServiceStub;
import com.ws.test.stubs.WeatherWebServiceStub.ArrayOfString;
import com.ws.test.stubs.WeatherWebServiceStub.GetSupportCity;
  
public class TestWeather {
  
    public static void main(String[] args) throws RemoteException {
        WeatherWebServiceStub stub = new WeatherWebServiceStub();
        GetSupportCity gsc = new GetSupportCity();
        gsc.setByProvinceName("山东");
        ArrayOfString as = stub.getSupportCity(gsc).getGetSupportCityResult();
        for (String city : as.getString()) {
            System.out.println(city);
        }
    }
}

 看控制台输出:

济南 (54823)
青岛 (54857)
淄博 (54830)
威海 (54774)
曲阜 (54918)
临沂 (54938)
烟台 (54765)
枣庄 (58024)
聊城 (54806)
济宁 (54915)
菏泽 (54906)
泰安 (54827)
日照 (54945)
东营 (54736)
德州 (54714)
滨州 (54734)
莱芜 (54828)
潍坊 (54843)

ok了,结束。

原文地址:https://www.cnblogs.com/firstdream/p/6394205.html