户端检验代码。新建Java Project,取名为ZZaxisClient。右键点击项目名,选择Properties-Java Build Path-Add Library-User Library-axis2。
8)新建package example.client。建立TestClient.java,代码如下。
基于Tomcat5.0和Axis2开发Web Service应用实例(4)
时间:2011-06-22 zhangjunhd
TestClient.java
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();
}
}
}
9)测试,run TestClient.java as Java Application。结果:
<hw:sayHelloResponse xmlns:hw="http://helloworld.com/"
xmlns:tns="http://ws.apache.org/axis2">
ZJHelloWorld!
</hw:sayHelloResponse>
本文出自 “子 孑” 博客,请务必保留此出处http://zhangjunhd.blog.51cto.com/113473/23690 |