只是个简单的例子,能说明问题就行。
服务端:
package com.ws.server;
import java.util.Iterator;
import javax.xml.stream.XMLStreamException;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMNode;
/**
* @company LuckyStar 工作室
*
* @description Axis2 WebService 服务端 (OM模式)
*
* @author 李彬
*
* @date 2009-8-12
*/
public class MyServer {
public OMElement invoke(OMElement element) throws XMLStreamException{
element.build();
Iterator<OMNode> iterator = element.getChildElements();
while(iterator.hasNext()){
OMNode node = iterator.next();
if (node.getType() == OMNode.ELEMENT_NODE) {
OMElement omElement = (OMElement)node;
if (omElement.getLocalName().equals("value")) {
String temp = omElement.getText().trim();
System.out.println("Service Value1:"+temp);
}else if(omElement.getLocalName().equals("value2")){
String temp = omElement.getText().trim();
System.out.println("Service Value2:"+temp);
}
}
}
element.detach();
return element;
}
}
服务端配置文件 META-INF/services.xml
<?xml version="1.0" encoding="utf-8"?>
<serviceGroup>
<service name="myservice">
<parameter name="ServiceClass" locked="false">com.ws.server.MyServer</parameter>
<operation name="invoke">
<messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"></messageReceiver>
<actionMapping>urn:invoke</actionMapping>
</operation>
</service>
</serviceGroup>
打包(aar包)放入%tomcat_home%/webapps/axis2/WEB-INF/service下,然后启动服务就oK了
下面是客户端
package com.ws.client;
import javax.xml.namespace.QName;
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.rpc.client.RPCServiceClient;
/**
* @company LuckyStar 工作室
*
* @description Axis2 WebService 客户端 (OM模式)
*
* @author 李彬
*
* @date 2009-8-12
*/
public class WsTest {
private static OMElement getInvokeOMElement(){
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace namespace = factory.createOMNamespace("http://www.liwu.com", "liwu");
OMElement method = factory.createOMElement("invoke",namespace);
OMElement value = factory.createOMElement("value",namespace);
value.addChild(factory.createOMText(value, "Hello terry!!!"));
OMElement value2 = factory.createOMElement("value",namespace);
value2.addChild(factory.createOMText(value2, "Hello wuxin!!"));
method.addChild(value);
method.addChild(value2);
return method;
}
@SuppressWarnings("unchecked")
public static void main(String[] args) {
EndpointReference endpointReference = new EndpointReference("http://localhost/axis2/services/myservice");
OMElement method = getInvokeOMElement();
Options options = new Options();
options.setTo(endpointReference);
ServiceClient client;
try {
client = new ServiceClient();
client.setOptions(options);
OMElement value = client.sendReceive(method);
System.out.println(value);
System.out.println();
Iterator<OMNode> iterator = value.getChildElements();
while(iterator.hasNext()){
OMNode node = iterator.next();
if (node.getType() == OMNode.ELEMENT_NODE) {
OMElement omElement = (OMElement)node;
if (omElement.getLocalName().equals("value")) {
String temp = omElement.getText().trim();
System.out.println(temp);
}
}
}
} catch (AxisFault e) {
e.printStackTrace();
}
}
}