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>
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();
}
}
}
|