写在前面

 本文只说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.1\samples\pojo\src\META-INF目录下有services.xml例子,可以复制过来修改。

6、打包

     选择包名和META-INF,export为server.jar,修改为servier.aar,扔进\webapps\axis2\WEB-INF\services目录下,重启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>

重新打包,扔进\webapps\axis2\WEB-INF\services目录下;

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

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

     先加入jar包,jar包路径:\axis2-1.7.1\lib\*.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 Files\axis2-1.7.1

path追加:%AXIS2_HOME%\bin

②在项目目录下,即跟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了,结束。

axis2 1.7.1使用教程的更多相关文章

  1. axis2之webservice

    Axis2之webservice超详细教程 Axis2是一套崭新的WebService引擎,该版本是对Axis1.x重新设计的产物.Axis2不仅支持SOAP1.1和SOAP1.2,还集成了非常流行的 ...

  2. axis2开发webservice总结

    需求环境:对接方公司提供wsdl文件,我方按照该wsdl文件开发服务端. 配置axis2开发环境,网上教程很多,不再啰嗦.环境搭好后执行wsdl2java -uri file:///C:/Users/ ...

  3. webservice的Axis2入门教程java版

    本文转自百度文库 Axis2是一套崭新的WebService引擎,该版本是对Axis1.x重新设计的产物.Axis2不仅支持SOAP1.1和SOAP1.2,还集成了非常流行的REST WebServi ...

  4. WebService引擎Axis2完美教程

    1.http://wenku.baidu.com/link?url=O05r69TGaLmrJrjJqGz-5EASox8FhhUO6i97xoUOV2dcbh8BEnOlX2EN3nuYGE_3HE ...

  5. Axis2的下载和安装

    Axis2是一套崭新的WebService引擎,该版本是对Axis1.x重新设计的产物.Axis2不仅支持SOAP1.1和SOAP1.2,还集成了非常流行的REST WebService,同时还支持S ...

  6. 如何使用axis2 构建 Android 服务器后端--- 工具准备与环境配置

    最近一个项目要做个android端的实验室器材管理系统.小伙伴英勇地接下android端的锅,我就 负责给他写后端,最近看到axis2 这个webservice挺好用的,折腾了几天给大家分享下: 1. ...

  7. axis2开发实例(一)

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

  8. WebService之Axis2(1):用POJO实现0配置的WebService

    Axis2是一套崭新的WebService引擎,该版本是对Axis1.x重新设计的产物.Axis2不仅支持SOAP1.1和SOAP1.2,还集成了非常流行的REST WebService,同时还支持S ...

  9. Python之Numpy详细教程

    NumPy - 简介 NumPy 是一个 Python 包. 它代表 “Numeric Python”. 它是一个由多维数组对象和用于处理数组的例程集合组成的库. Numeric,即 NumPy 的前 ...

随机推荐

  1. GETDATE()

    定义和用法 GETDATE() 函数从 SQL Server 返回当前的时间和日期. 语法 GETDATE() 实例 例子 1 使用下面的 SELECT 语句: SELECT GETDATE() AS ...

  2. matlab读取mysql数据

    conn = database('ci_bootstrap_3','root','1q1q1q1q','com.mysql.jdbc.Driver','jdbc:mysql://localhost:3 ...

  3. oracle启动停止命令

    dba 连接 oracle(切到 oracle 用户) sqlplus /nolog conn / as sysdba 登录成功之后,关闭数据库服务 shutdown immediate 登录成功之后 ...

  4. Javascript 基础夯实 —— 使用 webWorker 实现多线程(转)

    原文链接:https://zhuanlan.zhihu.com/p/29219879 当我们开始学习 javascript 的时候,我们就知道 js 其实是单线程的,所以当我们在浏览器中运行某些耗时算 ...

  5. ionic3中使用自定义配置

    新工作接触了ionic,以前没用过,只是离职前短暂接触过类似的vuex,到需要修改公司项目的时候临时差什么学什么,其中一个是自定义配置项 配置是很常见的设置,之前用的thinkphp的配置很清晰,基本 ...

  6. 自己定义ShareSDK分享平台界面

    自己定义ShareSDK分享平台界面 执行效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ2FvX2NodW4=/font/5a6L5L2T/fo ...

  7. caffe中ConvolutionLayer的前向和反向传播解析及源码阅读

    一.前向传播 在caffe中,卷积层做卷积的过程被转化成了由卷积核的参数组成的权重矩阵weights(简记为W)和feature map中的元素组成的输入矩阵(简记为Cin)的矩阵乘积W * Cin. ...

  8. 【VC编程技巧】窗口☞3.6以渐变效果加载对话框

    平时我们常常能够看到非常多应用程序启动过程非常酷.什么百叶窗.渐变,各种效果,今天我们看一下怎样在程序中添加这样的效果. 一.演示样例展示: watermark/2/text/aHR0cDovL2Js ...

  9. 内核调试神器SystemTap — 简单介绍与使用(一)

    a linux trace/probe tool. 官网:https://sourceware.org/systemtap/ 简单介绍 SystemTap是我眼下所知的最强大的内核调试工具,有些家伙甚 ...

  10. 【cl】工程导入

    File>Open 导入成功