webservice之axis2客户端调用(maven方式创建java项目)

1. 运行环境

   (1) 操作系统:window10

   (2)JDK:1.7

   (3) IDE:Myeclipse10

   (4) 服务器:tomcat7


2. 搭建步骤

    (1) 配置maven的配置文件,加载所需jar包

 pom.xml配置文件:

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4.0.0


  com.dh
  TestAxis2Client
  0.0.1-SNAPSHOT
  jar


  TestAxis2Client
  http://maven.apache.org


 
  
    UTF-8
    
    1.3
    1.2.5
    1.1.1
    1.6.2
    1.4.5
    3.1
    3.0
    
 



 
  


   backport-util-concurrent
   backport-util-concurrent
   ${backport-util-concurrent.version}

 
 

   commons-httpclient
   commons-httpclient
   ${commons-httpclient.version}

  
 

   org.apache.ws.commons.schema
   XmlSchema
   ${XmlSchema.version}

  
 

   commons-httpclient
   commons-httpclient
   ${commons-httpclient.version}

  
 

   wsdl4j
   wsdl4j
   ${wsdl4j.version}

  
 

   commons-logging
   commons-logging
   ${commons-logging.version}

  
 

   org.apache.ws.commons.axiom
   axiom
   ${axiom.version}

  
 

   org.apache.axis2
   axis2
   ${axis2.version}

  
   
      junit
      junit
      3.8.1
      test
   

 


(2) 编写测试类,有三种实现方式:

A. axiom实现客户端调用

package com.dh.axsi2client.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.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;


public class AXIOMClient {

private static String url = "http://localhost:8080/TestAxis2Service/services/hello";
private static String nps = "http://impl.service.axis2.test.com";


private static EndpointReference targetEPR = new EndpointReference(url);


public static OMElement getPassengerInfos(String symbol) {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(nps, "tns");
OMElement method = fac.createOMElement("hello", omNs);
OMElement value = fac.createOMElement("name", omNs);
value.addChild(fac.createOMText(value, symbol));
method.addChild(value);
return method;
}


public static void main(String[] args) {
try {
OMElement getPassenger = getPassengerInfos("wangtao1024");
Options options = new Options();
options.setTo(targetEPR);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
sender.sendRobust(getPassenger);
// 接口有返回值调用
/*OMElement result = sender.sendReceive(getPassenger);
String response = result.getFirstElement().getText();
System.err.println("Current passengers: " + response);*/
} catch (Exception e) {
e.printStackTrace();
}
}

}

B. ServiceClient方式调用

package com.dh.axsi2client.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.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;


public class TestAxis2Client {


/**
* @param args
*/
public static void main(String[] args) {
try {
String url = "http://localhost:8080/TestAxis2Service/services/hello?wsdl";
ServiceClient sc = new ServiceClient();
Options opts = new Options();
opts.setTo(new EndpointReference(url));
//opts.setAction("urn:hello");
opts.setTimeOutInMilliSeconds(10000);

sc.setOptions(opts);
//sc.sendReceive(createPayLoad());
sc.sendRobust(createPayLoad());
//OMElement res = sc.sendReceive(createPayLoad());
//System.out.println(res);
} catch (AxisFault e) {
e.printStackTrace();
}
}

public static OMElement createPayLoad() {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://impl.service.axis2.test.com", "");
OMElement method = fac.createOMElement("hello", omNs);
OMElement value = fac.createOMElement("name", omNs);
value.addChild(fac.createOMText(value, "wangtao"));
method.addChild(value);
method.build();
return method;


}


C. RPC方式客户端调用

package com.dh.axsi2client.client;


import javax.xml.namespace.QName;


import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;


public class TestRpcClient {


/**
* @param args
* @throws AxisFault 
*/
public static void main(String[] args) throws AxisFault {


String url = "http://localhost:8080/TestAxis2Service/services/hello";
String nps = "http://impl.service.axis2.test.com";
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(url);
options.setTo(targetEPR);
// 指定方法的参数值
Object[] requestParam = new Object[] { "wangtao" };
// 指定方法返回值的数据类型的Class对象
Class[] responseParam = new Class[] { String.class };
// 指定要调用的getGreeting方法及WSDL文件的命名空间
QName requestMethod = new QName(nps, "hello");
// 调用方法并输出该方法的返回值
try {
// 无返回值调用
serviceClient.invokeRobust(requestMethod, requestParam);
// 接口中有返回值调用
//System.out.println(serviceClient.invokeBlocking(requestMethod, requestParam, responseParam)[0]);
} catch (AxisFault e) {
e.printStackTrace();
}


}

}









你可能感兴趣的:(webservice之axis2客户端调用(maven方式创建java项目))