Eclipse+WebService开发案例

一、系统功能

开发一个计算器服务CalculateService,这个服务包含加(plus)、减(minus)、乘(multiply)、除(divide)的操作。

二、配置开发环境

到官网下载最新版本的Axis2,网址是http://axis.apache.org/axis2/java/core/download.cgi ,选择Binary Distribution的zip包,下载完成后,解压。
在Eclipse的菜单栏中,Window —> Preferences —> Web Service —> Axis2 Perferences,在Axis2 runtime location中选择Axis2解压缩包的位置,设置好后,点”OK”即行。(如图)
这里写图片描述

三、开发Web Service

(1)新建一个Java Project,命名为webservices。
(2)新建一个类,包名为com.ghs.service,类名为CaculateService。

package com.ghs.service;

public class CaculateService {

    /**
     * 加法运算
     * @param a
     * @param b
     * @return
     */
    public double plus(double a,double b){
        return a+b;
    }

    /**
     * 减法运算
     * @param a
     * @param b
     * @return
     */
    public double minus(double a,double b){
        return a-b;
    }

    /**
     * 乘法运算
     * @param a
     * @param b
     * @return
     */
    public double multiply(double a,double b){
        return a*b;
    }

    /**
     * 除法运算
     * @param a
     * @param b
     * @return
     */
    public double divide(double a,double b){
        if(b!=0){
            return a/b;
        }else{
            throw new IllegalArgumentException();
        }
    }
}

(3)在webservices项目名上右键,new—>other,找到”Web Services”下面的”Web Service”。
这里写图片描述

(4)下一步(next),在出现的Web Services对话框中,在Service implementation中点击”Browse”,进入Browse Classes对象框,查找到我们刚才写的写的CalculateService类。(如下图)。点击”ok”,则回到Web Service话框。
这里写图片描述

(5)在Web Service对话框中,将Web Service type中的滑调到”start service”的位置。在Web Service type滑块图的右边有个”Configuration”,点击它下面的选项,进入Service Deployment Configuration对象框,在这里选择相应的Server(我这里用Tomcat7.0)和Web Service runtime(选择Apache Axis2),如下图:
这里写图片描述

(6)将Client type中的滑块调到”Test client”的位置。同理,Client type中的滑块右边也有”Configuration”,也要进行相应的置,步骤同上。

(7)完成后,Next—> next即行。进入到Axis2 Web Service Java Bean Configuration,我们选择Generate a default services.xml,如下图所示:
这里写图片描述

(8)到了Server startup对话框,有个按键”start server”(如下图),点击它,则可启动Tomcat服务器了。
这里写图片描述

(9)服务器启动后,点击finish,就会打开Web Service Explorer,如下图所示:
这里写图片描述

(10)测试服务,例如,我们选择一个”plus”的Operation(必须是CalculateServiceSoap11Binding),出现下图,在a的输入框中输入1,在b的输入框中输入2,点击”go”,便会在status栏中显示结果3.0。其他方法的测试也类似。结果如上图所示:
这里写图片描述

四、CalculateService客户端调用程序

在上面的Web Service开发完成后,我们可以看到Eclipse为我们自动创建了一个webservicesClient,我们在里面新建一个CaculateServiceTest类。如下图所示:
这里写图片描述

package com.ghs.service;

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;

public class CaculateServiceTest {

    private static final String NAMESPACE = "http://service.ghs.com";

    public static void main(String[] args) throws AxisFault {
        //使用RPC方式调用WebService
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        //指定调用WebServices的URL
        EndpointReference reference = new EndpointReference("http://localhost:8080/webservices/services/CaculateService"); 
        options.setTo(reference);

        //指定要调用的方法及WSDL文件的命名空间
        QName plusQName = new QName(NAMESPACE,"plus");
        QName minusQName = new QName(NAMESPACE,"minus");
        QName multiplyQName = new QName(NAMESPACE,"multiply");
        QName divideQName = new QName(NAMESPACE,"divide");

        //指定要调用的方法的参数及返回类型
        Object[] params = new Object[]{1,2};
        Class[] classs = new Class[]{double.class};

        System.out.println("1+2 = "+serviceClient.invokeBlocking(plusQName, params, classs)[0]);
        System.out.println("1-2 = "+serviceClient.invokeBlocking(minusQName, params, classs)[0]);
        System.out.println("1*2 = "+serviceClient.invokeBlocking(multiplyQName, params, classs)[0]);
        System.out.println("1/2 = "+serviceClient.invokeBlocking(divideQName, params, classs)[0]);
    }
}

运行结果如下:

1+2 = 3.0
1-2 = -1.0
1*2 = 2.0
1/2 = 0.5
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值