POJO
在本文档中需要使用到的POJO有两个,一个是Weather.class,一个是WeatherService.class。其中Weather类中包含了关于天气的数据:Temperature(温度), forecast(预测), rain (是否下雨), and howMuchRain(雨量) (请见代码1:the Weather POJO)。
代码1:the Weather POJO
package sample.pojo.data;
public class Weather{
float temperature;
String forecast;
boolean rain;
float howMuchRain;
public void setTemperature(float temp){
temperature = temp;
}
public float getTemperature(){
return temperature;
}
public void setForecast(String fore){
forecast = fore;
}
public String getForecast(){
return forecast;
}
public void setRain(boolean r){
rain = r;
}
public boolean getRain(){
return rain;
}
public void setHowMuchRain(float howMuch){
howMuchRain = howMuch;
}
public float getHowMuchRain(){
return howMuchRain;
}
}
接下来是WeatherService.class 请见代码2:
代码2:the WeatherService POJO
package sample.pojo.service;
import sample.pojo.data.Weather;
public class WeatherService{
Weather weather;
public void setWeather(Weather weather){
this.weather = weather;
}
public Weather getWeather(){
return this.weather;
}
}
接下来,你需要知道为了把Web service部署在Apache Axis2 和 Tomcat 上还要做些什么。
POJO Web Service Using Apache Axis2 and Tomcat
已经编写好了POJO了?很好。这一部分将展示如何将一定结构的文件夹打包并发布成Web service。首先,我们需要进行对services.xml的配置,它用来对我们的Web service进行定义,然后我们使用Apache Ant对src文件夹进行打包,并把它部署在Tomcat上。
定义services.xml
为了让Axis2理解我们的web service是做什么的,我们必须编写相应的services.xml文件,请见代码3:
代码3:service定义文件:services.xml
<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>
service的名称为"WeatherService",范围为整个应用(application)。就如你在WeatherService.class中所见的,里面有2个方法:一个只有输入参数,一个既有输入也有输出参数。所以必须在messageReceivers元素中申明信息接受类型。最后"ServiceClass"参数的值必须把包名也包括进去:sample.pojo.service.WeatherService。当Web service中的操作被调用时,WeatherService.class中的相应方法就会被调用。接下来,我们去看看如何使用Ant来进行简单的构建。
Building the POJO Web Service Using Apache Ant
<property name="axis2.home" value="你的axis2安装根目录" />
接下来是需要构建的文件结构:
- WeatherService
- META-INF
- services.xml
- sample
- pojo
- data
- Weather.class
- service
- WeatherService.class
在cmd中用Ant对其进行构建之后,把生成的aar文件复制到%TOMCAT_HOME%\webapps\axis2\WEB-INF\services 文件夹之下。这样,Web service的部署就完成了。接下来进入测试阶段。
Testing the POJO Web Service Using RPCServiceClient
package sample.pojo.rpcclient;
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 {
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/axis2/services/WeatherService");
options.setTo(targetEPR);
// Setting the weather
QName opSetWeather =
new QName("http://service.pojo.sample/xsd", "setWeather");
Weather w = new Weather();
w.setTemperature((float)39.3);
w.setForecast("Cloudy with showers");
w.setRain(true);
w.setHowMuchRain((float)4.5);
Object[] opSetWeatherArgs = new Object[] { w };
serviceClient.invokeRobust(opSetWeather, opSetWeatherArgs);
...
加粗部分的代码,需要格外注意。EndpointReference变量需要设置为你所发布的web service的地址(可以通过启动Tomcat之后在浏览器中查看);opSetWeather变量是QName的一个实例,构造它时,第一个参数为对应的wsdl中的命名空间,第二个参数为需要调用的web service中的方法(此方法对应wsdl中operation元素的name);serviceClient.invokeRobust方法调用没有返回值的方法setWeather,并把对象型参数opSetWeatherArgs传递给它。接下来,我们来编写代码去获得天气数据,请见代码5:
...
serviceClient.invokeRobust(opSetWeather, opSetWeatherArgs);
// Getting the weather
QName opGetWeather =
new QName("http://service.pojo.sample/xsd","getWeather");
Object[] opGetWeatherArgs = new Object[] { };
Class[] returnTypes = new Class[] { Weather.class };
Object[] response = serviceClient.invokeBlocking(opGetWeather,
opGetWeatherArgs, returnTypes);
Weather result = (Weather) response[0];
if (result == null) {
System.out.println("Weather didn't initialize!");
return;
}
...
首先,设置opGetWeather的操作为getWeather。然后新建一个空对象用于放置输入参数(该方法没有输入参数),并新建一个范型数组returnTypes用来指定getWeather方法返回值的数据类型的class对象。之后调用invokeBlocking方法,并新建一个Object数组实例response用来获取方法返回值,若返回值为空,则在控制台打印出提示。接着,我们在控制台上显示返回结果,见代码6:
...
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());
}
}
rpc.client.run:
[java] Temperature : 39.3
[java] Forecast : Cloudy with showers
[java] Rain : true
[java] How much rain (in inches) : 4.5