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();
}
}
}
|
package sample;
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 {
//读取client端getSayHelloOMElement()方法传递的参数in。
public OMElement sayHello(OMElement in){
//将in转换为String。
String name=in.getText();
String info=name+"HelloWorld!";
//创建response SOAP包。
OMFactory fac=OMAbstractFactory.getOMFactory();
// OMNamespace指定此SOAP文档名称空间。
OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
//创建元素sayHello,并指定其在omNs指代的名称空间中。
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>
// ServiceClass指定Java Class的位置,即实现服务的类。
<parameter name="ServiceClass" locked="false">sample.HelloWorld</parameter>
// operation与Java Class中方法名对应。
<operation name="sayHello">
// messageReceiver看下文注解。
<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 {
// targetEPR指定打包的Service(.aar文件)在容器中的物理位置。
private static EndpointReference targetEPR=new EndpointReference
("http://localhost:8080/axis2/services/HelloWorld");
public static OMElement getSayHelloOMElement(){
//创建request SOAP包。
OMFactory fac=OMAbstractFactory.getOMFactory();
// OMNamespace指定此SOAP文档名称空间。
OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
//创建元素sayHello,并指定其在omNs指代的名称空间中。
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();
//发出request SOAP,
//同时将得到的远端由sayHello方法返回的信息保存到result。
//通过services.xml能准确找到sayHello方法所在的文件。
OMElement result=sender.sendReceive(sayHello);
}
catch(Exception axisFault){
axisFault.printStackTrace();
}
}
}
|