axis2传递简单java对象(POJO)

转载:[url]http://tenn.iteye.com/blog/100759[/url]
在Axis2_1.2版本中提供了传递Java对象的功能(注:只有1.1/1.2版本提供,更早的Axis2版本没有此功能)。此项功能称为传输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.相应的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开发Web Service应用实例 )。

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.结果
Temperature               : 39.3
Forecast : Cloudy with showers
Rain : true
How much rain (in inches) : 4.5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值