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

来源:互联网 发布:教育大数据应用 编辑:程序博客网 时间:2024/05/16 06:53

1. 运行环境

   (1) 操作系统:window10

   (2)JDK:1.7

   (3) IDE:Myeclipse10

   (4) 服务器:tomcat7


2. 搭建步骤

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

 pom.xml配置文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>


  <groupId>com.dh</groupId>
  <artifactId>TestAxis2Client</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>


  <name>TestAxis2Client</name>
  <url>http://maven.apache.org</url>


  <properties>
  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
    <axis2.version>1.3</axis2.version>
    <axiom.version>1.2.5</axiom.version>
    <commons-logging.version>1.1.1</commons-logging.version>
    <wsdl4j.version>1.6.2</wsdl4j.version>
    <XmlSchema.version>1.4.5</XmlSchema.version>
    <commons-httpclient.version>3.1</commons-httpclient.version>
    <backport-util-concurrent.version>3.0</backport-util-concurrent.version>
    
  </properties>


  <dependencies>
  
<!-- https://mvnrepository.com/artifact/backport-util-concurrent/backport-util-concurrent -->
<dependency>
   <groupId>backport-util-concurrent</groupId>
   <artifactId>backport-util-concurrent</artifactId>
   <version>${backport-util-concurrent.version}</version>
</dependency>
 
  <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
   <groupId>commons-httpclient</groupId>
   <artifactId>commons-httpclient</artifactId>
   <version>${commons-httpclient.version}</version>
</dependency>
  
  <!-- https://mvnrepository.com/artifact/org.apache.ws.commons.schema/XmlSchema -->
<dependency>
   <groupId>org.apache.ws.commons.schema</groupId>
   <artifactId>XmlSchema</artifactId>
   <version>${XmlSchema.version}</version>
</dependency>
  
  <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
<dependency>
   <groupId>commons-httpclient</groupId>
   <artifactId>commons-httpclient</artifactId>
   <version>${commons-httpclient.version}</version>
</dependency>
  
  <!-- https://mvnrepository.com/artifact/wsdl4j/wsdl4j -->
<dependency>
   <groupId>wsdl4j</groupId>
   <artifactId>wsdl4j</artifactId>
   <version>${wsdl4j.version}</version>
</dependency>
  
  <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
   <groupId>commons-logging</groupId>
   <artifactId>commons-logging</artifactId>
   <version>${commons-logging.version}</version>
</dependency>
  
  <!-- https://mvnrepository.com/artifact/org.apache.ws.commons.axiom/axiom -->
<dependency>
   <groupId>org.apache.ws.commons.axiom</groupId>
   <artifactId>axiom</artifactId>
   <version>${axiom.version}</version>
</dependency>
  
  <!-- https://mvnrepository.com/artifact/org.apache.axis2/axis2 -->
<dependency>
   <groupId>org.apache.axis2</groupId>
   <artifactId>axis2</artifactId>
   <version>${axis2.version}</version>
</dependency>
  
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>


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


}

}









阅读全文
0 1
原创粉丝点击