使用Java调用Web Service

使用Axis2-1.6.1实现第一个Web Service一文中发布了一个SimpleService,现在分别使用RPC方法以及wsdl2java在客户端调用该Web Service服务。

使用RPC方式(6)步

在eclipse中新建一个项目,并在package包中新建RPCClient类
1.创建RPCServiceClient对象
2.创建EndpointReference对象,并指定要访问的Web Service的URL(不包含wsdl)
3.创建描述Web Service方法参数值的Object[]对象
4.创建Web Service方法返回值类型的Class[]对象
5.创建QNAME对象,并指定要调用的Web Service方法
6.使用RPCServiceClient类的invokeBlocking方法调用Web Service。

package client;
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 RPCClient {
    public static void main(String[] args) throws Exception {
        //第1步
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        //第2步
        EndpointReference targetEPR = new EndpointReference(
                "http://localhost:8080/axis2/services/SimpleService/");
        options.setTo(targetEPR);
        Object[] opAddEntryArgs = new Object[]{"superman"};//第3步
        Class[] classes = new Class[]{String.class};//第4步
        //在创建QName对象时,QNAME类的构造方法的第一个参数表示WSDL的命名空间,也就是<wsdl:definitions>元素的targetNamespace属性值。
        QName opAddEntry = new QName("http://ws.apache.org/axis2","getGreeting");//第5步
        System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,classes)[0]);//第6步
        classes = new Class[]{int.class};
        opAddEntry = new QName("http://ws.apache.org/axis2","getPrice");
        System.out.println(serviceClient.invokeBlocking(opAddEntry,new Object[]{},classes)[0]);//第二个参数表示要调用的web service方法的参数值,无参不能用null,应该用长度为0的object数组
        //如果调用的Web Service方法没有返回值,应该使用RPCServiceClient类的invokeRobust方法,该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同
    }

}
invokeBlocking方法采用了同步的方式来调用Web Service,异步方式另表。
不过这种方式调用Web Service方法相当麻烦,Axis2提供wsdl2java命令,该命令可以根据WSDL的内容自动生成调用Web Service客户端的stub类,然后客户端就可以像调用本地类一样使用stub类。

使用wsdl2java命令简化客户端编写

使用wsdl2java命令之前,建议先设置一下AXIS2_HOME环境变量。系统变量名:AXIS2_HOME,变量值及上一篇中下载的axis2-1.6.1-bin.zip解压后的根目录所在地址,我是解药到D盘,地址为D:\axis2-1.6.1\。

使用如下的命令生成客户端的stub类:
%AXIS2_HOME%\bin\wsdl2java -uri http://localhost:8080/axis2/services/SimpleService?wsdl -p client -s -o stub
*其中-uri指定了wsdl文件的地址(可以是本地路径,也可以是web路径),-p指定了生成的stub类的包名, -o指定了存放stub类的目录名
*注意:Web Service方法的返回值和参数值都封装在了相应的类中,例如:getGreeting方法的返回值封装在GetGreetingResponse类中,参数值封装在GetGreeting类中。
将使用上述命令后得到的stub目录下生成的SimpleServiceStub.java复制到eclipse工程中,新建StubSimpleClient.java类,代码如下:

package client;

import org.apache.axis2.AxisFault;

public class StubSimpleClient {

    public static void main(String[] args) throws Exception {
        SimpleServiceStub stub = new SimpleServiceStub();
        SimpleServiceStub.GetGreeting gg = new SimpleServiceStub.GetGreeting();
        gg.setArgs0(" leolee");
        System.out.println(stub.getGreeting(gg).local_return);
        System.out.println(stub.getPrice(new SimpleServiceStub.GetPrice()).local_return);
    }

}

运行后控制台得到hello leolee和一个随机数,调用Web Service成功

webservice如何使用其他的类

  • (1)如果在web service类中需要import其他的类,那么需要将这些类放在
package test;
public class MyClass{
    private String name;
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
}

SimpleService类需要做相应修改:

import test.MyClass;
public class SimpleService{
    public String getGreeting(String name){
        return "hello " + name;
    }
    public int getPrice(){
        return new java.util.Random().nextInt(1000);
    }
    public MyClass getMyClass(){
        MyClass myclass = new  MyClass();
        myclass.setName("lilei");
        return myclass;
        }
}

编译MyClass.java和SimpleService.java:

javac -d . MyClass.java SimpleService.java

将test目录及目录中的MyClass.class文件复制到WEB-INF\classes目录中,将SimpleService.class复制到WEB-INF\pojo目录中。修改StubSimpleClient.java类,在最后增加一行代码

package client;

import org.apache.axis2.AxisFault;

public class StubSimpleClient {

    public static void main(String[] args) throws Exception {
        SimpleServiceStub stub = new SimpleServiceStub();
        SimpleServiceStub.GetGreeting gg = new SimpleServiceStub.GetGreeting();
        gg.setArgs0(" leolee");
        System.out.println(stub.getGreeting(gg).local_return);
        System.out.println(stub.getPrice(new SimpleServiceStub.GetPrice()).local_return);
        System.out.println(stub.getMyClass(new SimpleServiceStub.GetMyClass()).local_return.getName());
    }

}
运行后控制台显示结果:

hello leolee
837
lilei

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值