第一,RPC方式,不生成客户端代码,引入相应的axis2的jar包(不好用)
注意:暂时没有成功调用,没有参数传递时远程调用成功,当有参数传递时远程调用失败;
package com.ming.axis2;
import javax.xml.namespace.QName;
import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.junit.Test;
/**
* 方法一: 应用rpc的方式调用 这种方式就等于远程调用, 即通过url定位告诉远程服务器,告知方法名称,参数等, 调用远程服务,得到结果。
* 使用org.apache.axis2.rpc.client.RPCServiceClient类调用WebService;
*
* 【注】:
* 如果被调用的WebService方法有返回值 应使用 invokeBlocking 方法 该方法有三个参数
* 第一个参数的类型是QName对象,表示要调用的方法名;
* 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[],
* 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{};
* 第三个参数表示WebService方法的 返回值类型的Class对象,参数类型为Class[]。
*
* 如果被调用的WebService方法没有返回值 应使用 invokeRobust方法,
* 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。
*
*/
public class MobileClientRPC {
/**
*
* @Title: testRPCClient
* @Description: TODO(rpc远程调用,失败,无法正确传参)
* @return void 返回类型
*/
@Test
public void testRPCClient() {
try {
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
// 创建WSDL的URL,注意不是服务地址
String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl";
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(url);
Options options = serviceClient.getOptions();
// 确定目标服务地址
options.setTo(targetEPR);
// 确定调用方法(wsdl 命名空间地址 (wsdl文档中的targetNamespace) 和 方法名称 的组合)
options.setAction("http://WebXml.com.cn/getMobileCodeInfo");
// 指定方法的参数值
Object[] parameters = new Object[] {"1866666666", ""};
// 创建服务名称
// 1.namespaceURI - 命名空间地址 (wsdl文档中的targetNamespace)
// 2.localPart - 服务视图名 (wsdl文档中operation的方法名称,例如)
QName qname = new QName("http://WebXml.com.cn/", "getMobileCodeInfo");
// 调用方法一 传递参数,调用服务,获取服务返回结果集
OMElement element = serviceClient.invokeBlocking(qname, parameters);
System.out.println(element);
/*
* 值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。
* 我们可以对之灵活应用,下面我取第一个元素值,并打印之。因为调用的方法返回一个结果
*/
String result = element.getFirstElement().getText();
System.out.println(result);
// 调用方法二 getPrice方法并输出该方法的返回值
// 指定方法返回值的数据类型的Class对象
Class[] returnTypes = new Class[] {String.class};
Object[] response = serviceClient.invokeBlocking(qname, parameters, returnTypes);
String r = (String) response[0];
System.out.println(r);
} catch (AxisFault e) {
e.printStackTrace();
}
}
/**
*
* @Title: Weather
* @Description: TODO(rpc远程调用,成功,没有参数能够成功)
* @return void 返回类型
* @throws AxisFault
*/
@Test
public void Weather() throws AxisFault{
//使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
//指定调用WebService的URL
EndpointReference targetEPR =
new EndpointReference("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl");
options.setTo(targetEPR);
options.setAction("http://WebXml.com.cn/getRegionProvince");
//指定方法的参数值
Object[] opAddEntryArgs = new Object[] {};
//指定要调用的方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://WebXml.com.cn/", "getRegionProvince");
//调用法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs));
}
/**
*
* @Title: Weather1
* @Description: TODO(rpc远程调用,失败,单个参数失败)
* @return void 返回类型
*/
@Test
public void Weather1() {
try {
//使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
//指定调用WebService的URL
EndpointReference targetEPR =
new EndpointReference("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl");
options.setTo(targetEPR);
options.setAction("http://WebXml.com.cn/getSupportCityDataset");
//指定方法的参数值
Object[] opAddEntryArgs = new Object[] {"北京"};
//指定要调用的方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://WebXml.com.cn/", "getSupportCityDataset");
//调用法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs));
} catch (AxisFault e) {
e.printStackTrace();
}
}
}
第二,应用document方式调用 用ducument方式应用现对繁琐而灵活,引入相应的axis2的jar包。现在用的比较多。
即使用org.apache.axis2.client.ServiceClient类进行远程调用web服务,不生成客户端(推荐使用)
package com.ming.axis2;
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.junit.Test;
/**
*
* @ClassName: MobileClientDoc
* @Description: TODO
* 方法二: 应用document方式调用 用ducument方式应用现对繁琐而灵活。现在用的比较多。因为真正摆脱了我们不想要的耦合
* 即使用org.apache.axis2.client.ServiceClient类进行远程调用web服务,不生成客户端
*
* @date 2017年11月9日 下午1:27:17
*
*/
public class MobileClientDoc {
/**
*
* @Title: Weather
* @Description: TODO(document远程调用,成功,没有参数能够成功)
* @return void 返回类型
* @throws AxisFault
*/
@Test
public void Weather() throws AxisFault{
ServiceClient serviceClient = new ServiceClient();
//创建服务地址WebService的URL,注意不是WSDL的URL
String url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx";
EndpointReference targetEPR = new EndpointReference(url);
Options options = serviceClient.getOptions();
options.setTo(targetEPR);
//确定调用方法(wsdl 命名空间地址 (wsdl文档中的targetNamespace) 和 方法名称 的组合)
options.setAction("http://WebXml.com.cn/getRegionProvince");
OMFactory fac = OMAbstractFactory.getOMFactory();
/*
* 指定命名空间,参数:
* uri--即为wsdl文档的targetNamespace,命名空间
* perfix--可不填
*/
OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "");
// 指定方法
OMElement method = fac.createOMElement("getSupportCityDataset", omNs);
method.build();
//远程调用web服务
OMElement result = serviceClient.sendReceive(method);
System.out.println(result);
}
/**
*
* @Title: Weather1
* @Description: TODO(document远程调用,成功,没有参数能够成功)
* @return void 返回类型
*/
@Test
public void Weather1() {
try {
ServiceClient serviceClient = new ServiceClient();
//创建服务地址WebService的URL,注意不是WSDL的URL
String url = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx";
EndpointReference targetEPR = new EndpointReference(url);
Options options = serviceClient.getOptions();
options.setTo(targetEPR);
//确定调用方法(wsdl 命名空间地址 (wsdl文档中的targetNamespace) 和 方法名称 的组合)
options.setAction("http://WebXml.com.cn/getSupportCityDataset");
OMFactory fac = OMAbstractFactory.getOMFactory();
/*
* 指定命名空间,参数:
* uri--即为wsdl文档的targetNamespace,命名空间
* perfix--可不填
*/
OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "");
// 指定方法
OMElement method = fac.createOMElement("getSupportCityDataset", omNs);
// 指定方法的参数
OMElement theRegionCode = fac.createOMElement("theRegionCode", omNs);
theRegionCode.setText("北京");
method.addChild(theRegionCode);
method.build();
//远程调用web服务
OMElement result = serviceClient.sendReceive(method);
System.out.println(result);
} catch (AxisFault axisFault) {
axisFault.printStackTrace();
}
}
/**
*
* @Title: Weather1
* @Description: TODO(document远程调用,成功,多个参数)
* @return void 返回类型
*/
@Test
public void MobileCodeWS() {
try {
ServiceClient serviceClient = new ServiceClient();
//创建服务地址WebService的URL,注意不是WSDL的URL
String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";
EndpointReference targetEPR = new EndpointReference(url);
Options options = serviceClient.getOptions();
options.setTo(targetEPR);
//确定调用方法(wsdl 命名空间地址 (wsdl文档中的targetNamespace) 和 方法名称 的组合)
options.setAction("http://WebXml.com.cn/getMobileCodeInfo");
OMFactory fac = OMAbstractFactory.getOMFactory();
/*
* 指定命名空间,参数:
* uri--即为wsdl文档的targetNamespace,命名空间
* perfix--可不填
*/
OMNamespace omNs = fac.createOMNamespace("http://WebXml.com.cn/", "");
// 指定方法
OMElement method = fac.createOMElement("getMobileCodeInfo", omNs);
// 指定方法的参数
OMElement mobileCode = fac.createOMElement("mobileCode", omNs);
mobileCode.setText("15932582632");
OMElement userID = fac.createOMElement("userID", omNs);
userID.setText("");
method.addChild(mobileCode);
method.addChild(userID);
method.build();
//远程调用web服务
OMElement result = serviceClient.sendReceive(method);
System.out.println(result);
} catch (AxisFault axisFault) {
axisFault.printStackTrace();
}
}
}
第三,用wsdl2java工具,生成客户端方式调用webservice服务
步骤:
(1)引入axis2的相应jar包;
(2)下载axis2-1.7.4-bin.zip,在bin目录中有wsdl2java,用于根据WSDL生成相应的服务端和客户端代码的生成工具;
wsdl2java 用于根据WSDL生成相应的服务端和客户端代码的生成工具。
命令行格式为:WSDL2Java [options] -uri : A url or path to a WSDL
例如:
wsdl2java -uri http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl -s -p com.ming.mobile -o build\client
wsdl2java -uri http://localhost:8080/cxfService_0617/services/Hellows?wsdl -s -o build\client
其中常用的options具体如下:
-o : 指定生成代码的输出路径
-a : 生成异步模式的代码
-s : 生成同步模式的代码
-p : 指定代码的package名称
-l : 使用的语言(Java/C) 默认是java
-t : 为代码生成测试用例
-ss : 生成服务端代码 默认不生成
-sd : 生成服务描述文件 services.xml,仅与-ss一同使用
-d : 指定databingding,例如,adb,xmlbean,jibx,jaxme and jaxbri
-g : 生成服务端和客户端的代码
-pn : 当WSDL中有多个port时,指定其中一个port
-sn : 选择WSDL中的一个service
-u : 展开data-binding的类
-r : 为代码生成指定一个repository
-ssi : 为服务端实现代码生成接口类
-S : 为生成的源码指定存储路径
-R : 为生成的resources指定存储路径
–noBuildXML : 输出中不生成build.xml文件
–noWSDL : 在resources目录中不生成WSDL文件
–noMessageReceiver : 不生成MessageReceiver类
(3)调用实例如下;
package com.ming.axis2;
import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;
import org.junit.Test;
import com.ming.mobile.MobileCodeWSStub;
import com.ming.mobile.MobileCodeWSStub.GetMobileCodeInfo;
import com.ming.mobile.MobileCodeWSStub.GetMobileCodeInfoResponse;
/**
*
* @ClassName: MobileClientWsdl2
* @Description: TODO(利用axis2插件wsdl2java生成客户端方式调用,只生成一个类,其中含有很多内部类)
* @date 2017年11月9日 下午2:14:03
*
*/
public class MobileClientWsdl2 {
@Test
public void testCodeClient() {
try {
// web服务地址,不是wsdl地址
String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";
//创建客户端实体类
MobileCodeWSStub stub = new MobileCodeWSStub(url);
//创建调用方法的实体,设置参数
GetMobileCodeInfo getMobileCodeInfo = new GetMobileCodeInfo();
getMobileCodeInfo.setMobileCode("15932582632");
getMobileCodeInfo.setUserID("");
//客户端调用方法,并返回xml信息
GetMobileCodeInfoResponse mobileCodeInfo = stub.getMobileCodeInfo(getMobileCodeInfo);
System.out.println(mobileCodeInfo.getGetMobileCodeInfoResult());
} catch (AxisFault e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}