Apache Axis客户端调用WEBSERVICE三种方式

本文介绍了使用Apache Axis客户端调用WebService的三种方式:动态接口调用(DII),动态代理以及通过WSDL生成的存根。通过具体的Java代码示例展示了如何进行调用并获取返回结果。

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

 
l
三种方式
=================================
  动态调用接口:
import java.net.URL;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class UsingDII {
    public static void main(String[] args) {
        try {
            int a = 100, b=60;
            String endPoint = "http://localhost:9000/testaxis/AddFunction.jws";
            Service service = new Service();
            Call call = (Call)service.createCall();
            call.setOperationName(new QName(endPoint,"addInt"));
            call.setTargetEndpointAddress(new URL(endPoint));
            Integer result = (Integer)call.invoke(new Object[]{new Integer(a),new Integer(b)});
            System.out.println("result is :"+result);        
        } catch (Exception e) {
            e.printStackTrace();
        }     
    }
}
====================================================================================
使用动态代理 Dynamic Proxy
 
import java.net.URL;
import javax.xml.*;
public class UsingDP {
    public static void main(String[] args) {
        try {
            int a = 100, b=60;
            String wsdlUrl = "http://localhost:9000/testaxis/AddFunction.jws?wsdl";
            String nameSpaceUri = "http://localhost:9000/testaxis/AddFunction.jws";
            String serviceName = "AddFunctionService";
            String portName = "AddFunction";
            ServiceFactory serviceFactory = ServiceFactory.newInstance();        
            Service service = serviceFactory.createService(new URL(wsdlUrl),new QName(nameSpaceUri,serviceName));
            AddFunctionServiceIntf adsIntf = (AddFunctionServiceIntf)service.getPort(new QName(nameSpaceUri,portName),AddFunctionServiceIntf.class);        
            System.out.println("result is :"+adsIntf.addInt(a, b));       
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
需要定义一个接口
import java.rmi.Remote;
public interface AddFunctionServiceIntf extends Remote{
    Integer addInt(int a, int b);
}
===================================================
使用从WSDL生成的存根generated Stubs from Service WSDL description
 
package _18._15._21._163.axis.services.AddFunction1Service;
public class AddFunction1Client
{
   public static void main(String [] args) {
       try {
   BeanService.Complex a = new BeanService.Complex();
   BeanService.Complex b = new BeanService.Complex();
   a.setR(10.0);
   a.setI(5.0);
   b.setR(3.0);
   b.setI(2.0);
   AddFunction1Service afs = new AddFunction1ServiceLocator();
   AddFunction1 af = afs.getAddFunction1Service();
   BeanService.Complex ret = af.addComplex(a, b);
           System.out.println("addComplex(a + b) = (" + ret.getR() + ", " + ret.getI() + ")");
       } catch (Exception e) {
           System.err.println("Execution failed. Exception: " + e);
       }
   }
}
l 生成存根:
==================FINISH===============================
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值