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

版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。 http://zhangjunhd.51cto.com/113473/26053
本文介绍如何使用Axis2Web Service中传递Java对象。<o:p></o:p>
author: ZJ <st1:chsdate w:st="on" year="2007" month="5" day="7" islunardate="False" isrocdate="False">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.基于此POJOservice<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 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>
<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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值