调用webservice方法总结

本文深入解析了调用WebService的几种方法,包括stub方式和动态代理(使用axis1.4和axis2)。通过示例代码,详细阐述了每种方法的实现流程,帮助开发者灵活选择合适的调用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现在有几种调用webservice的方法,总结一下:

1、         stub方式。这种方式,是利用axis的WSDL2Java工具类。这个是根据webservice的wsdl生成客户端类。这样就可以像是在调用本地的类一样来调用webservice,很简单。需要一个批处理,如下:

set Axis_Lib=E:\jar\axis-bin-1_4\axis-1_4\lib

set Java_Cmd=java-Djava.ext.dirs=%Axis_Lib%

set Output_Path=D:\src

set Package=com.hsipcc.wsdl

%Java_Cmd%org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% http://localhost:8000/service/Business?wsdl

这会在D盘的src目录下生成stub源码,接下来就可以在工程里用了。但是这种方法不灵活,就是比较笨,如果service端更改了,这里需要手动的更改stub。

2、         动态代理:

2.1、axis1.4。

package com.aming.client;

 

import java.net.MalformedURLException;

import java.net.URL;

import java.rmi.RemoteException;

import javax.xml.namespace.QName;

import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

 

 

 

public class Axis1Client {

 

    public static void main(String[] args) throws ServiceException,MalformedURLException, RemoteException {

        String path = "http://localhost:8080/xfire/services/staticService";

        Service service = new Service(); 

        Call call = (Call)service.createCall(); 

        call.setTargetEndpointAddress(new URL(path)); 

        call.setOperationName(new QName(path ,"putInfo")); 

        Object obj =call.invoke(new Object[]{"ladygaga","123","456"}); 

        System.out.println(obj.toString()); 

 

    }

 

}

解释一下:编写流程:

1、         创建axis的客户端服务service

2、         由service创建call(call就是要进行调用的对象)

3、         需要设置call对象的属性targetEndpointAddress(目标地址)和operationName(操作方法就是要调用的方法,其中这个方法需要QName(用于定位方法的)对象),new QName(path,”putInfo”)其中path是名称空间,putInfo是这个名称空间中的一个方法(要调用的方法)。

4、         最后就是调用webservice的方法了,invoke,这个方法需要传入需要的传入的参数,并且这个参数以对象数组的方式传过去。此方法会返回返回值。

2.2  axis2

  packagecom.aming.test;

 

import org.apache.axis2.AxisFault;

importorg.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import javax.xml.namespace.QName;

importorg.apache.axis2.rpc.client.RPCServiceClient;

 

public class DiaoYong {

 

 

       publicstatic void main(String[] args) throws AxisFault {

                  //  使用RPC方式调用 WebService   

                RPCServiceClient serviceClient = newRPCServiceClient();

               Options options =serviceClient.getOptions();

               // 指定调用WebService的URL

               EndpointReference targetEPR = newEndpointReference(

                      "http://localhost:8080/xfire/services/staticService");

               options.setTo(targetEPR);

               // 指定getGreeting方法的参数值

               Object[] opAddEntryArgs = new Object[]{"超人","123","456"};

               // 指定getGreeting方法返回值的数据类型的Class对象

               Class[] classes = new Class[]{int.class};

               // 指定要调用的getGreeting方法及WSDL文件的命名空间

               QName opAddEntry = newQName("http://com.hundsun.service/StaticService","putInfo");

               //  调用getGreeting方法并输出该方法的返回值

              System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);

               // 下面是调用getPrice方法的代码,这些代码与调用getGreeting方法的代码类似

            // classes = new Class[] {int.class};

//           QName opAddEntry = newQName("http://localhost:8080/axis1/services/MyService","getInfo");

//          System.out.println(serviceClient.invokeBlocking(opAddEntry, newObject[]{}, classes)[0]);

       }

 

}

注释详细,不解释了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值