上篇文章里,说到pojo内,class不能有包名,这样的要求,肯定是不符现实开发的。显然,apache也是允许带包的类发布webservice的,不然不是没人用啦。
1.先写一个简单的计算器 带多个方法,带参数,有返回值的
package com.zy.axis2.test; public class CalculateService { // 加法 public Double plus(Double x, Double y) { return x + y; } // 减法 public Double minus(Double x, Double y) { return x - y; } // 乘法 public Double multiply(Double x, Double y) { return x * y; } // 除法 public Double divide(Double x, Double y) { if (y != 0) return x / y; else return -1.0; } }
2.编写server.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- 服务名称 --> <service name="CalculateService"> <!-- 服务描述 --> <description> 算术运算的服务 </description> <!-- 设置服务类 --> <parameter name="ServiceClass"> com.zy.axis2.test.CalculateService </parameter> <operation name="plus"> <!-- mep 固定写法,class:RPCMessageReceiver(带返回值的处理器) RPCInOnlyMessageReceiver(不带返回值的处理器)--> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> <operation name="minus"> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> <operation name="multiply"> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> <operation name="divide"> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> </service>
放到meta-inf目录下(直接使用eclipse打包,然后在打开jar文件,把serverm.xml丢进去),修改jar后缀为aar后缀
我打的名字是server.jar,更改后缀为aar,所以最后是server.aar,Axis2建议使用aar发布WebService,
将server.aar放到<Tomcat安装目录>/webapps/axis2/WEB-INF/services下,访问http://localhost:8080/axis2/services/listServices,
多出了一个CalculateService,说明发布成功。
分别访问
http://localhost:8080/axis2/services/CalculateService/plus?x=1&y=2
http://localhost:8080/axis2/services/CalculateService/divide?x=1&y=2
http://localhost:8080/axis2/services/CalculateService/minus?x=1&y=2
http://localhost:8080/axis2/services/CalculateService/multiply?x=1&y=2
比如
我们要严格遵守参数类型和值,假如访问的参数的错误的,会报错
也可以发布多个WebService,可以使用serviceGroup标签。