Axis2 Service Client

運用Axis 2呼叫Web Service的Client端範例程式:

import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.client.Options;
import org.apache.axis2.addressing.EndpointReference;

public class ServiceClientExample {

public static void main(String[] args) throws Exception {
ServiceClient sc = new ServiceClient();
// create option object
Options opts = new Options();
//setting target EPR
opts.setTo(new EndpointReference(
“http://127.0.0.1:8080/axis2/services/HelloService”));
//Setting action
opts.setAction(“urn:sayHello”);
//setting created option into service client
sc.setOptions(opts);
OMElement res = sc.sendReceive(createPayLoad());
System.out.println(res);
}

public static OMElement createPayLoad() {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
“http://ws.apache.org/axis2”, “ns1”);
OMElement method = fac.createOMElement(“sayHello”, omNs);
OMElement value = fac.createOMElement(“name”, omNs);
value.setText(“Albert”);
method.addChild(value);
return method;
}
}

需下載axis2 1.6.1的binary檔: http://axis.apache.org/axis2/java/core/download.cgi

上述範例會用到之jar檔包含:
Compile階段 (javac):
axiom-api-1.2.13.jar;axiom-impl-1.2.13.jar;axis2-kernel-1.6.2.jar
執行階段 (java):
axiom-api-1.2.13.jar;axiom-impl-1.2.13.jar;axis2-kernel-1.6.2.jar;commons-logging-1.1.1.jar;wsdl4j-1.6.2.jar;XmlSchema-1.4.7.jar;neethi-3.0.2.jar;mail-1.4.jar;axis2-adb-1.6.2.jar;axis2-transport-http-1.6.2.jar;axis2-transport-local-1.6.2.jar;axis2-transport-http-1.6.2.jar;commons-httpclient-3.1.jar;httpcore-4.0.jar;commons-codec-1.3.jar