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===============================