Axis2客户端编程
一,使用axis2开发一个web服务。 MyService.java public class MyService { public String echo(String value){ return value; } public void update(int value){ System.out.println("value is :"+value); } }
services.xml
打包并发布该web服务。
二,客户端调用
import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient;
public class Test { public static void main(String args) { try { ServiceClient sc = new ServiceClient(); Options opts = new Options(); opts.setTo(new EndpointReference("http://localhost:8888/axis2/services/MyService")); opts.setAction("urn:echo"); sc.setOptions(opts); OMElement res = sc.sendReceive(createPayLoad()); System.out.println(res); } catch (AxisFault e) { e.printStackTrace(); }
}
public static OMElement createPayLoad(){
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://ws.apache.org/axis2", "nsl");
OMElement method = fac.createOMElement("echo",omNs);
OMElement value = fac.createOMElement("value",omNs);
value.setText("Hello,my first service utilization");
method.addChild(value);
return method;
}
}