于Apache官网下载Axis2最新版本 1.5.1 (ws.apache.org/axis2/download.cgi)
分别下载Standard Binary Distribution、WAR (Web Archive) Distribution、Documents Distribution三个zip包。
?
安装Axis2:有两种方式:
- 将Axis2作为一个单独的Server安装:解压Standard Binary Distribution,配置环境变量AXIS2_HOME?以及添加%AXIS2_HOME?%/bin至PATH路径(注意Axis2使用jdk1.5或更高版本);切换目录至AXIS2_HOME?/bin下,使用axis2server.bat启动Axis2,使用http://localhost:8080/axis2/services/连接看安装是否成功。
- 将Axis2载入Servlet容器(Tomcat)中使用:将下载的axis2.war文件copy至TOMCAT_HOME/webapps目录下,启动Tomcat即可,使用http://localhost:8080/axis2 查看安装是否成功(Tomcat使用默认端口880)。
学些使用Axis2和Tomcat构建POJO WS:
结合官网教程POJO Guide 以及Axis2目录下的axis2-1.5.1\samples\pojo为例学习,在这里使用Apache ANT进行打包部署(我使用的ant版本为1.6.2)。
解释Service定义文件services.xml:
? 指定scope为application且name为WeatherService的一个ws,参数ServiceClass指定WeatherService的class。?As you can see in the WeatherService POJO, there are two methods: IN-ONLY method and IN-OUT method. Hence the messageReceiver elements are ordered within the messageReceivers tag.(不太明白这句话以及两个method的类型 in-only in-out)。
<service name="WeatherService" scope="application">
<description>
Weather POJO Service
</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">sample.pojo.service.WeatherService</parameter>
</service>
?
使用ant generate.service 命令生成WeatherService的aar文件,使用Tomcat-Axis2 admin控制台upload该aar文件,或者直接将aar文件copy至<tomcat-home>/webapps/axis2/WEB-INF/services目录下。
测试客户端:
? ?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;
import sample.pojo.data.Weather;
public class WeatherRPCClient {
public static void main(String[] args1) throws AxisFault {
// 使用RPC(Remote Process Call)方式调用WeatherService
RPCServiceClient serviceClient = new RPCServiceClient();
// 操作客户端的选项???
Options options = serviceClient.getOptions();
// 指定调用WeatherService的URL
EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/WeatherService");
options.setTo(targetEPR);
// 指定要调用的setWeather方法以及WSDL文件的命名空间
QName opSetWeather = new QName("http://service.pojo.sample", "setWeather");
// weather data
Weather w = new Weather();
w.setTemperature((float)39.3);
w.setForecast("Cloudy with showers");
w.setRain(true);
w.setHowMuchRain((float)4.5);
// 指定setWeather方法的参数值
Object[] opSetWeatherArgs = new Object[] { w };
// invokeRobust
serviceClient.invokeRobust(opSetWeather, opSetWeatherArgs);
// 指定要调用的getWeather方法以及WSDL文件的命名空间
QName opGetWeather = new QName("http://service.pojo.sample", "getWeather");
// 指定getWeather方法的参数值
Object[] opGetWeatherArgs = new Object[] { };
// 指定getWeather方法返回值的数据类型的Class对象
Class[] returnTypes = new Class[] { Weather.class };
// invokeBlocking 取得返回结果
Object[] response = serviceClient.invokeBlocking(opGetWeather,
opGetWeatherArgs, returnTypes);
Weather result = (Weather) response[0];
if (result == null) {
System.out.println("Weather didn't initialize!");
return;
}
// Displaying the result
System.out.println("Temperature : " +
result.getTemperature());
System.out.println("Forecast : " +
result.getForecast());
System.out.println("Rain : " +
result.getRain());
System.out.println("How much rain (in inches) : " +
result.getHowMuchRain());
}
}
?
?
使用RPCServiceClient测试POJO WS:
? ?运行结果如下:
Temperature?????????????? : 39.3 ? ? ? ? ? ?
Forecast????????????????? : Cloudy with showers
Rain????????????????????? : true
How much rain (in inches) : 4.5
?
?
?