四、使用Axis2传递简单Java对象(POJO)

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。 http://zhangjunhd.blog.51cto.com/113473/26053
本文介绍如何使用Axis2 Web Service 中传递Java 对象。<o:p></o:p>
author: ZJ <st1:chsdate year="2007" month="5" day="7" islunardate="False" isrocdate="False" w:st="on">07-5-7</st1:chsdate>
<o:p> </o:p>
Axis2_1.2 版本中提供了传递 Java 对象的功能(注:只有 1.1/1.2 版本提供,更早的 Axis2 版本没有此功能)。此项功能称为传输 POJO(a Plain Old Java Object)
<o:p> </o:p>
1. 引入一个简单的POJO- The Weather POJO<o:p></o:p>
Weather.java<o:p></o:p>
package sample.pojo.data;
<o:p> </o:p>
public class Weather {
float temperature;
String forecast;
boolean rain;
float howMuchRain;
<o:p> </o:p>
public void setTemperature(float temp) {
temperature = temp;
}
<o:p> </o:p>
public float getTemperature() {
return temperature;
}
<o:p> </o:p>
public void setForecast(String fore) {
forecast = fore;
}
<o:p> </o:p>
public String getForecast() {
return forecast;
}
<o:p> </o:p>
public void setRain(boolean r) {
rain = r;
}
<o:p> </o:p>
public boolean getRain() {
return rain;
}
<o:p> </o:p>
public void setHowMuchRain(float howMuch) {
howMuchRain = howMuch;
}
<o:p> </o:p>
public float getHowMuchRain() {
return howMuchRain;
}
}
Note that it's all just straight POJOs with field items and getter and setter methods for each field.<o:p></o:p>
<o:p> </o:p>
2. 基于此POJO service<o:p></o:p>
WeatherService.java<o:p></o:p>
package sample.pojo.service;
<o:p> </o:p>
import sample.pojo.data.Weather;
<o:p> </o:p>
public class WeatherService{
Weather weather;
public void setWeather(Weather weather){
this.weather = weather;
}
<o:p> </o:p>
public Weather getWeather(){
return this.weather;
}
}
<o:p> </o:p>
3. 相应的services.xml<o:p></o:p>
<service scope="application" name="WeatherService"></service>
<description>Weather POJO Service</description>
<messagereceivers></messagereceivers>
<messagereceiver></messagereceiver>
mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
<messagereceiver></messagereceiver>
mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
<parameter name="ServiceClass"></parameter>
sample.pojo.service.WeatherService
<o:p> </o:p>
4. 打包与部署<o:p></o:p>
将文件组织成:
- WeatherService<o:p></o:p>
- META-INF<o:p></o:p>
- services.xml<o:p></o:p>
- sample<o:p></o:p>
- pojo<o:p></o:p>
- data<o:p></o:p>
- Weather.class<o:p></o:p>
- service<o:p></o:p>
- WeatherService.class
将其打包为 WeatherService.aar ,并部署在 Tomcat 上(详见 基于Tomcat5.0和Axis2开发Web Service应用实例 )。
<o:p> </o:p>
5 .测试<o:p></o:p>
WeatherRPCClient.java<o:p></o:p>
package sample.pojo.rpcclient;
<o:p> </o:p>
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;<o:p></o:p>
import org.apache.axis2.rpc.client.RPCServiceClient;
import sample.pojo.data.Weather;
<o:p> </o:p>
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);
<o:p> </o:p>
// 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);
<o:p> </o:p>
Object[] opSetWeatherArgs = new Object[] { w };
serviceClient.invokeRobust(opSetWeather, opSetWeatherArgs);
serviceClient.invokeRobust(opSetWeather, opSetWeatherArgs);
<o:p> </o:p>
// Getting the weather
QName opGetWeather = new QName("http://service.pojo.sample/xsd",
"getWeather");
<o:p> </o:p>
Object[] opGetWeatherArgs = new Object[] {};
Class[] returnTypes = new Class[] { Weather.class };
Object[] response = serviceClient.invokeBlocking(opGetWeather,
opGetWeatherArgs, returnTypes);
<o:p> </o:p>
Weather result = (Weather) response[0];
if (result == null) {
System.out.println("Weather didn't initialize!");
return;
}
<o:p> </o:p>
// 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());
<o:p> </o:p>
}
}
<o:p> </o:p>
6 .结果<o:p></o:p>
Temperature : 39.3
Forecast : Cloudy with showers
Rain : true
How much rain (in inches) : 4.5
<o:p> </o:p>

本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/26053

你可能感兴趣的:(java,apache,tomcat,xml,Blog)