写在前面

 本文只说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. eas之EntityViewInfo对象mainQuery中查询条件

    EntityViewInfo对象mainQuery中查询条件:  添加查询字段:(Sql语句中的selectz子句内容)    SelecttorItemCollection sic=new Sele ...

  2. Golang - 处理json

    目录 Golang - 处理json 1. 编码json 2. 解码json Golang - 处理json 1. 编码json 使用json.Marshal()函数可以对一组数据进行JSON格式的编 ...

  3. 6.在idea中链接数据库

    1.打开数据库配置的窗口 File - view  - Tool Windows - Database 2.打开mysql的数据库链接模板 Database - + - Data Source - M ...

  4. 游标后面select 带有in时

    今天遇到一个问题,使用游标时,在给游标填充值的时候,select  语句中带有 where查询条件,并且还有 in子句. 本来我是这样写的,试了很多次都不出结果,当然number in (304010 ...

  5. 微信小程序获取登录手机号

    小程序获取登录用户手机号. 因为需要用户主动触发才能发起获取手机号接口,所以该功能不由 API 来调用,需用 <button> 组件的点击来触发. 首先,放置一个 button 按钮,将 ...

  6. redis命令学习的注意问题

    1.set get命令只用于字符串,get命令取key值时string正常返回,没有key返回nil,其他类型会报错 设置的时候是set test redis ex 200000等同于SETEX te ...

  7. LOJ #2542 [PKUWC2018]随机游走 (概率期望、组合数学、子集和变换、Min-Max容斥)

    很好很有趣很神仙的题! 题目链接: https://loj.ac/problem/2542 题意: 请自行阅读 题解首先我们显然要求的是几个随机变量的最大值的期望(不是期望的最大值),然后这玩意很难求 ...

  8. 设置Jmeter默认中文页面

    下载安装好Jmeter后默认的是英文,对于我这种学渣来说简直就是受到了1000000点攻击. 所以,如何把英文界面换成中文呢? 方法一(从网上看到的) 启动Jmeter找到 options >c ...

  9. Swift中文教程(二)基本运算符

    1.基本运算符 运算符是一种特定的符号或表达式,用来检验.改动或合并变量.比如,用求和运算符+能够对两个数字进行求和(如let i = 1 + 2):略微复杂一点的样例有逻辑与操作符&& ...

  10. STM32学习之路-LCD(4)&lt;显示字符&gt;

    昨晚疯狂的打了一夜的LOL,感觉L多了,今天一天精神萎靡.还是继续把显示字符给看了,可是在犹豫要不要写这篇文章 事实上写的东西也就是copy别人家的代码,不想写那么多,就记录下自己困惑的地方吧.也许改 ...