Terry.Li-彬

虚其心,可解天下之问;专其心,可治天下之学;静其心,可悟天下之理;恒其心,可成天下之业。

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  143 随笔 :: 344 文章 :: 130 评论 :: 0 Trackbacks
版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://zhangjunhd.51cto.com/113473/26053
本文介绍如何使用Axis2Web Service中传递Java对象。
author: ZJ 07-5-7
 
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.基于此POJOservice
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
posted on 2008-07-10 17:23 礼物 阅读(1976) 评论(0)  编辑  收藏 所属分类: web serviceAxis