您的位置:首页 > 运维架构 > Apache

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

2005-12-04 15:58 417 查看
 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生成存根: java org.apache.axis.wsdlWSDL2Java http://localhost:9000/testaxis/services/AddFunction1Service?wsdl==================FINISH===============================
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息