1.server端代码
?
package com.service; import javax.xml.stream.XMLStreamException; 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 SampleService { public OMElement sayHello(OMElement element) throws XMLStreamException { element.build(); element.detach(); /* 这是客户端发过来的包文,根节点一定是sayHello: * <example1:sayHello xmlns:example1="http://example1.org/example1"> * <example1:personToGreet>John</example1:personToGreet> * </example1:sayHello> */ String rootName = element.getLocalName(); System.out.println("Reading rootElement:" + rootName); OMElement childElement = element.getFirstElement(); String personToGreet = childElement.getText(); /* 注意:这里除了跟节点名sayHelloResponse,其他可以随意组装xml: * <example1:sayHelloResponse xmlns:example1="http://example1.org/example1"> * <example1:greeting>Hello, John</example1:greeting> * </example1:sayHelloResponse> */ OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://example1.org/example1", "example1"); OMElement method = fac.createOMElement("sayHelloResponse", omNs); OMElement value = fac.createOMElement("greeting", omNs); fac.createOMText(value, "Hello, " + personToGreet); method.addChild(value); return method; } @SuppressWarnings("unused") private void ping() { } }
?
?
?
2.services.xml
?
<service name="SampleService"> <description> This is a sample service created in the Axis2 User's Guide </description> <parameter name="ServiceClass"> com.service.SampleService </parameter> <operation name="sayHello"> <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" /> </operation> <operation name="ping"> <messageReceiver class="org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver" /> </operation> </service>?
?
3. 客户端代码
?
package com.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.Constants; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; public class TestOME { private static EndpointReference targetEPR = new EndpointReference( "http://localhost:8888/tservice/services/SampleService"); public static OMElement getBody(String personToGreet) { /* 注意:这里根节点必须是sayHello,其他可以任意组装xml报文: * <example1:sayHello xmlns:example1="http://example1.org/example1"> * <example1:personToGreet>John</example1:personToGreet> * </example1:sayHello> */ OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://example1.org/example1", "example1"); OMElement method = fac.createOMElement("sayHello", omNs); OMElement value = fac.createOMElement("personToGreet", omNs); value.addChild(fac.createOMText(value, personToGreet)); method.addChild(value); return method; } public static void main(String[] args) { try { Options options = new Options(); options.setTo(targetEPR); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); ServiceClient sender = new ServiceClient(); sender.setOptions(options); System.out.println("req:" + getBody("John")); OMElement result = sender.sendReceive(getBody("John")); System.out.println("res:" + result); /* 这是服务端还回的报文,根节点一定是sayHelloResponse: * <example1:sayHelloResponse xmlns:example1="http://example1.org/example1"> * <example1:greeting>Hello, John</example1:greeting> * </example1:sayHelloResponse> */ String response = result.getFirstElement().getText(); System.out.println(response); } catch (Exception e) { System.out.println(e.toString()); } } }?
?
4,运行client结果
req:<example1:sayHello xmlns:example1="http://example1.org/example1"><example1:personToGreet>John</example1:personToGreet></example1:sayHello> res:<example1:sayHelloResponse xmlns:example1="http://example1.org/example1"><example1:greeting>Hello, John</example1:greeting></example1:sayHelloResponse> Hello, John
?
?