您的位置:首页 > 其它

AXIS调用webService服务

2015-12-17 17:12 309 查看
package testClick.src.test;

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.client.ServiceClient;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.junit.Test;

import testClick.src.testclick.HelloWebService;
import testClick.src.testclick.HelloWebServicePortType;

public class TestWebservice {

@Test
public void Test(){
//testClient();
//testRPC();
testOMElement();
}

/**
* 第一种
* MyEclipse 生成客户端方式
*/
public void testClient(){
HelloWebService service = new HelloWebService();
HelloWebServicePortType portType = service.getHelloWebServiceHttpSoap11Endpoint();
String result = portType.sayHello("jack");
System.out.println(result);
}

/**
* 	第二种
*  使用axis2 RPC调用webservice 需要导入相应的jar包
*/
public void testRPC(){
try{
RPCServiceClient client = new RPCServiceClient();
Options options = client.getOptions();
String url = "http://localhost:8080/axis2/services/HelloWebService?wsdl";
EndpointReference end = new EndpointReference(url);
options.setTo(end);

Object[] obj = new Object[]{"tom"};
Class<?>[] classes = new Class[] { String.class };

QName qname = new QName("http://testClick", "sayHello");
String result = (String) client.invokeBlocking(qname, obj,classes)[0];
System.out.println(result);
}catch(AxisFault e){
e.printStackTrace();
}
}

/**
*  第三种
* @throws AxisFault
*/
public void testOMElement(){
try{
ServiceClient sc = new ServiceClient();
Options opts = new Options();
String url = "http://localhost:8080/axis2/services/HelloWebService?wsdl";
EndpointReference end = new EndpointReference(url);
opts.setTo(end);
opts.setAction("sayHello");
sc.setOptions(opts);

OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://testClick", "");
OMElement method = fac.createOMElement("sayHello",omNs);
OMElement value = fac.createOMElement("name",omNs);
value.setText("admin");
method.addChild(value);
OMElement res = sc.sendReceive(method);
res.getFirstElement().getText();
System.out.println(res.getFirstElement().getText());
}catch(AxisFault e){
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  web service axis