本文共 3756 字,大约阅读时间需要 12 分钟。
import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; public class HelloWorld { public OMElement sayHello(OMElement in){ String name=in.getText(); String info=name+"HelloWorld!"; OMFactory fac=OMAbstractFactory.getOMFactory(); OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw"); OMElement resp=fac.createOMElement("sayHelloResponse",omNs); resp.setText(info); return resp; } } |
<?xml version="1.0" encoding="UTF-8"?> <service name="HelloWorld"> <description> This is a sample Web Service. </description> <parameter name="ServiceClass" locked="false">sample.HelloWorld</parameter> <operation name="sayHello"> <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/> </operation> </service> |
package example.client; 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.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; public class TestClient { private static EndpointReference targetEPR=new EndpointReference ("http://localhost:8080/axis2/services/HelloWorld"); public static OMElement getSayHelloOMElement(){ OMFactory fac=OMAbstractFactory.getOMFactory(); OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw"); OMElement method=fac.createOMElement("sayHello",omNs); method.setText("ZJ"); return method; } public static void main(String[] args){ try{ Options options=new Options(); options.setTo(targetEPR); ServiceClient sender=new ServiceClient(); sender.setOptions(options); OMElement sayHello=TestClient.getSayHelloOMElement(); OMElement result=sender.sendReceive(sayHello); System.out.println(result); } catch(Exception axisFault){ axisFault.printStackTrace(); } } } |
<hw:sayHelloResponse xmlns:hw="http://helloworld.com/" xmlns:tns="http://ws.apache.org/axis2"> ZJHelloWorld! </hw:sayHelloResponse> |