Terry.Li-彬

          虛其心,可解天下之問;專其心,可治天下之學;靜其心,可悟天下之理;恒其心,可成天下之業(yè)。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            143 隨筆 :: 344 文章 :: 130 評論 :: 0 Trackbacks
          版權(quán)聲明:原創(chuàng)作品,允許轉(zhuǎn)載,轉(zhuǎn)載時請務(wù)必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。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.相應(yīng)的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開發(fā)Web Service應(yīng)用實例 )。
           
          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.結(jié)果
          Temperature               : 39.3
          Forecast                  : Cloudy with showers
          Rain                     : true
          How much rain (in inches)    : 4.5
          posted on 2008-07-10 17:23 禮物 閱讀(2005) 評論(0)  編輯  收藏 所屬分類: web service 、Axis
          主站蜘蛛池模板: 晋宁县| 河池市| 嘉义县| 德化县| 大庆市| 搜索| 铁岭县| 亚东县| 洱源县| 宁蒗| 陵川县| 宜丰县| 淮滨县| 甘南县| 电白县| 明光市| 宁武县| 湄潭县| 资溪县| 东辽县| 左贡县| 蕲春县| 磴口县| 丹棱县| 平定县| 贞丰县| 江西省| 兴和县| 陆丰市| 阿图什市| 石渠县| 瑞安市| 儋州市| 保靖县| 大宁县| 巴东县| 梧州市| 巴楚县| 清河县| 新和县| 定兴县|