随笔-72  评论-20  文章-0  trackbacks-1
在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
 

posted on 2007-08-19 05:07 前方的路 阅读(2531) 评论(4)  编辑  收藏 所属分类: Java技术

评论:
# re: 使用Axis2传递简单Java对象(POJO)[未登录] 2008-04-24 23:04 | x
serviceClient.invokeRobust(opSetWeather, opSetWeatherArgs);
serviceClient.invokeRobust(opSetWeather, opSetWeatherArgs);
为什么要重复?  回复  更多评论
  
# re: 使用Axis2传递简单Java对象(POJO) 2008-08-01 10:12 | zhugw
serviceClient.invokeRobust(opSetWeather, opSetWeatherArgs);
serviceClient.invokeRobust(opSetWeather, opSetWeatherArgs);
这两行不需要  回复  更多评论
  
# re: 使用Axis2传递简单Java对象(POJO) 2008-08-27 21:19 | gxl
你好,我想请教一下。
为什么 我在eclipse里 写客户端的代码的时候,import org.apache.axis2.rpc.client.RPCServiceClient总是提示找不到包呢,我该到哪里导入呢?
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options这几个都可以找到的。  回复  更多评论
  
# re: 使用Axis2传递简单Java对象(POJO) 2011-01-08 15:19 | 路边帅小子
在你的axis2文件的bin目录下呀
  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: