在Axis2_1.2版本中提供了傳遞Java對象的功能(注:只有1.1/1.2版本提供,更早的Axis2版本沒有此功能)。此項(xiàng)功能稱為傳輸POJO(a Plain Old Java Object)
1.引入一個簡單的POJO- The Weather POJO
Weather.java
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;
}
}
|
Note that it's all just straight POJOs with field items and getter and setter methods for each field.
2.基于此POJO的service
WeatherService.java
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;
}
}
|
3.相應(yīng)的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>
|
4.打包與部署
將文件組織成:
- WeatherService
- META-INF
- services.xml
- sample
- pojo
- data
- Weather.class
- service
- WeatherService.class
|
將其打包為WeatherService.aar,并部署在Tomcat上(詳見 基于Tomcat5.0和Axis2開發(fā)Web Service應(yīng)用實(shí)例 )。
5.測試
WeatherRPCClient.java
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);
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;
}
// 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());
}
}
|
6.結(jié)果
Temperature : 39.3
Forecast : Cloudy with showers
Rain : true
How much rain (in inches) : 4.5
|