Web service 学习笔记(1):利用Apache Axis2来部署发布POJO型Web Service

POJO

在本文档中需要使用到的POJO有两个,一个是Weather.class,一个是WeatherService.class。其中Weather类中包含了关于天气的数据:Temperature(温度), forecast(预测), rain (是否下雨), and howMuchRain(雨量) (请见代码1:the Weather POJO)。

代码1:the Weather POJO

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;
    }
}

接下来是WeatherService.class 请见代码2:

代码2:the WeatherService POJO

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;
    }
}

接下来,你需要知道为了把Web service部署在Apache Axis2 和 Tomcat 上还要做些什么。


POJO Web Service Using Apache Axis2 and Tomcat

已经编写好了POJO了?很好。这一部分将展示如何将一定结构的文件夹打包并发布成Web service。首先,我们需要进行对services.xml的配置,它用来对我们的Web service进行定义,然后我们使用Apache Ant对src文件夹进行打包,并把它部署在Tomcat上。

定义services.xml

为了让Axis2理解我们的web service是做什么的,我们必须编写相应的services.xml文件,请见代码3:

代码3:service定义文件: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>

service的名称为"WeatherService",范围为整个应用(application)。就如你在WeatherService.class中所见的,里面有2个方法:一个只有输入参数,一个既有输入也有输出参数。所以必须在messageReceivers元素中申明信息接受类型。最后"ServiceClass"参数的值必须把包名也包括进去:sample.pojo.service.WeatherService。当Web service中的操作被调用时,WeatherService.class中的相应方法就会被调用。接下来,我们去看看如何使用Ant来进行简单的构建。


Building the POJO Web Service Using Apache Ant

对于Ant,在这里就不进行敷述了。在你对src文件进行构建之前,你需要去这里下载Axis2 1.6.1-bin 以及 1.6.1-war。之后以C:\app\axis2-1.6.1\samples\pojoguide中的build.xml文件作为蓝本,并对以下代码行进行修改:
<property name="axis2.home" value="你的axis2安装根目录" />

接下来是需要构建的文件结构:
 - WeatherService
   - META-INF
     - services.xml
   - sample
     - pojo
       - data
         - Weather.class
       - service
         - WeatherService.class

在cmd中用Ant对其进行构建之后,把生成的aar文件复制到%TOMCAT_HOME%\webapps\axis2\WEB-INF\services 文件夹之下。这样,Web service的部署就完成了。接下来进入测试阶段。


Testing the POJO Web Service Using RPCServiceClient

接下来我们需要编写一个控制台程序进行测试,首先我们new一个Weather对象,并对其属性进行设置,请见代码4:
代码4:设置Weather对象
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);
...

加粗部分的代码,需要格外注意。EndpointReference变量需要设置为你所发布的web service的地址(可以通过启动Tomcat之后在浏览器中查看);opSetWeather变量是QName的一个实例,构造它时,第一个参数为对应的wsdl中的命名空间,第二个参数为需要调用的web service中的方法(此方法对应wsdl中operation元素的name);serviceClient.invokeRobust方法调用没有返回值的方法setWeather,并把对象型参数opSetWeatherArgs传递给它。接下来,我们来编写代码去获得天气数据,请见代码5:
代码5:获取Weather的数据
...
        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;
        }
...
首先,设置opGetWeather的操作为getWeather。然后新建一个空对象用于放置输入参数(该方法没有输入参数),并新建一个范型数组returnTypes用来指定getWeather方法返回值的数据类型的class对象。之后调用invokeBlocking方法,并新建一个Object数组实例response用来获取方法返回值,若返回值为空,则在控制台打印出提示。接着,我们在控制台上显示返回结果,见代码6:
代码6:显示返回结果
...
            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());
        
    }
}

显示结果应该如下:
rpc.client.run:
     [java] Temperature               : 39.3
     [java] Forecast                  : Cloudy with showers
     [java] Rain                      : true
     [java] How much rain (in inches) : 4.5



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值