ToB企服应用市场:ToB评测及商务社交产业平台
标题:
利用idea天生webservice客户端--详解步骤--(wsdl文件的使用)
[打印本页]
作者:
盛世宏图
时间:
2024-10-30 11:44
标题:
利用idea天生webservice客户端--详解步骤--(wsdl文件的使用)
目录
一、idea安装webservice
1.点击左上file,选中settings编辑
2.下载Web Service
3.给此项目添加webservice
4.添加webservice的依赖
二、利用idea根据wsdl文件自动天生webService客户端代码(然后比照着天生的测试类举行接口或方法的调用)
1.打开tools -> WebServices -> Generate Java Code From Wsdl,按照图中序次举行
2.按照图中序次举行利用,末了点击OK
3.必须勾选Generate TestCase
4.wsdl文件转换天生Java代码:成功
5.如何使用天生的代码
三、直接利用Axis2调用wsdl范例接口:
一、idea安装webservice
1.点击左上file,选中settings
2.下载Web Service
3.给此项目添加webservice
4.添加webservice的依赖
依赖加就行镜像用阿里云的基本都能下载
<!-- axis 1.4 jar start -->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-saaj</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.4</version>
</dependency>
复制代码
<!--webservice的:TestCase必要的jar包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
二、利用idea根据wsdl文件自动天生webService客户端代码(然后比照着天生的测试类举行接口或方法的调用)
步骤:1.利用idea根据wsdl文件天生代码,必要先将wsdl文件下载到当地
2.下载时无须要要求,选择Apache Axis,依赖必要自行百度搜刮
3.下载后必要比照着测试类代码,写调用接口或方法的代码
好处:1.入参的参数不必要本身举行封装 , 特别轻易写
2.返回值都被封装好了 , 可以很轻易就取到 , 不用本身写解析代码
1.打开tools -> WebServices -> Generate Java Code From Wsdl,按照图中序次举行
2.按照图中序次举行利用,末了点击OK
3.必须勾选Generate TestCase
(会给出调用接口的例子,照着抄就可以实现调用指定接口)
4.2点击ok报错 , 就去百度下载Axis所需的依赖 , 第一次需额外下载一个插件(直接下载完就行)
只要不是报wsdl文件出错 , 其他报错后,只要能天生代码 , 就可以使用
4.wsdl文件转换天生Java代码:成功
例子1:
例子2:
5.如何使用天生的代码
点击打开名字中带Test的这个类,找到你必要的方法,直接复制出来就能用
三、直接利用Axis2调用wsdl范例接口:
好处:不用利用idea根据wsdl自动天生webService客户端代码,然后再比照着测试类调用所需方法
坏处: 1.入参必要本身举行参数的封装 ,本身封装参数有例子也不轻易
2.如果必要获取调用接口的返回值,则还不会解析返回的数据,无法得到所需的参数(没解析报文的例子,本身不会写)
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.rpc.client.RPCServiceClient;
public class Test {
public static void test() {
String userId = "123";
String bindAccount = "123";
RPCServiceClient serviceClient = null;
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace omDiag = factory.createOMNamespace("http://diagnosis.interfaces.axis2.osf.nort hbound.neal.cpehg.ums.zte.com", "diag");
OMNamespace omXSD = factory.createOMNamespace("http://model.common.northbound.neal.cpehg. ums.zte.com/xsd", "xsd");
try {
serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference("http://10.46.60.200:9094/axis2/services/Cpe112Diag nosisWebServices?wsdl");
options.setTo(targetEPR);
options.setTimeOutInMilliSeconds(30000);
options.setManageSession(true);
// 指定方法的参数值
OMElement paramRequest = factory.createOMElement("request", omDiag);
OMElement paramUserId = factory.createOMElement("userID", omXSD);
paramUserId.setText(userId);
OMElement paramBindAccount = factory.createOMElement("bindAccount", omXSD);
paramBindAccount.setText(bindAccount);
paramRequest.addChild(paramBindAccount);
paramRequest.addChild(paramUserId);
OMElement paramItemName = factory.createOMElement("itemName", omDiag);
paramItemName.setText("cpehg.diagnosis.CpeBasicInfo");
String method = "getParameterValuesFromDbAndCpeByItemName";
OMElement data = factory.createOMElement(method, omXSD);
data.setNamespace(omDiag);
data.addChild(paramRequest);
data.addChild(paramItemName);
OMElement re = serviceClient.sendReceive(data);
// 处理返回数据
} catch (AxisFault e) {
// 异常处理
e.printStackTrace();
} finally {
try {
if (serviceClient != null) serviceClient.cleanupTransport();
} catch (AxisFault e) {
}
}
}
}
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4