利用idea天生webservice客户端--详解步骤--(wsdl文件的使用) ...

打印 上一主题 下一主题

主题 882|帖子 882|积分 2646

目录

一、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的依赖

  1. 依赖加就行镜像用阿里云的基本都能下载
  2. <!-- axis 1.4 jar start -->
  3. <dependency>
  4.     <groupId>org.apache.axis</groupId>
  5.     <artifactId>axis</artifactId>
  6.     <version>1.4</version>
  7. </dependency>
  8. <dependency>
  9.     <groupId>commons-discovery</groupId>
  10.     <artifactId>commons-discovery</artifactId>
  11.     <version>0.2</version>
  12.     <exclusions>
  13.         <exclusion>
  14.             <groupId>commons-logging</groupId>
  15.             <artifactId>commons-logging</artifactId>
  16.         </exclusion>
  17.     </exclusions>
  18. </dependency>
  19. <dependency>
  20.     <groupId>org.apache.axis</groupId>
  21.     <artifactId>axis-jaxrpc</artifactId>
  22.     <version>1.4</version>
  23. </dependency>
  24. <dependency>
  25.     <groupId>org.apache.axis</groupId>
  26.     <artifactId>axis-saaj</artifactId>
  27.     <version>1.4</version>
  28. </dependency>
  29. <dependency>
  30.     <groupId>wsdl4j</groupId>
  31.     <artifactId>wsdl4j</artifactId>
  32.     <version>1.4</version>
  33. </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 调用接口示例:
      

   
  1. import org.apache.axiom.om.OMAbstractFactory;
  2. import org.apache.axiom.om.OMElement;
  3. import org.apache.axiom.om.OMFactory;
  4. import org.apache.axiom.om.OMNamespace;
  5. import org.apache.axis2.AxisFault;
  6. import org.apache.axis2.addressing.EndpointReference;
  7. import org.apache.axis2.client.Options;
  8. import org.apache.axis2.rpc.client.RPCServiceClient;
  9. public class Test {
  10.     public static void test() {
  11.         String userId = "123";
  12.         String bindAccount = "123";
  13.         RPCServiceClient serviceClient = null;
  14.         OMFactory factory = OMAbstractFactory.getOMFactory();
  15.         OMNamespace omDiag = factory.createOMNamespace("http://diagnosis.interfaces.axis2.osf.nort hbound.neal.cpehg.ums.zte.com", "diag");
  16.         OMNamespace omXSD = factory.createOMNamespace("http://model.common.northbound.neal.cpehg. ums.zte.com/xsd", "xsd");
  17.         try {
  18.             serviceClient = new RPCServiceClient();
  19.             Options options = serviceClient.getOptions();
  20. // 指定调用WebService的URL
  21.             EndpointReference targetEPR = new EndpointReference("http://10.46.60.200:9094/axis2/services/Cpe112Diag nosisWebServices?wsdl");
  22.             options.setTo(targetEPR);
  23.             options.setTimeOutInMilliSeconds(30000);
  24.             options.setManageSession(true);
  25. // 指定方法的参数值
  26.             OMElement paramRequest = factory.createOMElement("request", omDiag);
  27.             OMElement paramUserId = factory.createOMElement("userID", omXSD);
  28.             paramUserId.setText(userId);
  29.             OMElement paramBindAccount = factory.createOMElement("bindAccount", omXSD);
  30.             paramBindAccount.setText(bindAccount);
  31.             paramRequest.addChild(paramBindAccount);
  32.             paramRequest.addChild(paramUserId);
  33.             OMElement paramItemName = factory.createOMElement("itemName", omDiag);
  34.             paramItemName.setText("cpehg.diagnosis.CpeBasicInfo");
  35.             String method = "getParameterValuesFromDbAndCpeByItemName";
  36.             OMElement data = factory.createOMElement(method, omXSD);
  37.             data.setNamespace(omDiag);
  38.             data.addChild(paramRequest);
  39.             data.addChild(paramItemName);
  40.             OMElement re = serviceClient.sendReceive(data);
  41. // 处理返回数据
  42.         } catch (AxisFault e) {
  43. // 异常处理
  44.             e.printStackTrace();
  45.         } finally {
  46.             try {
  47.                 if (serviceClient != null) serviceClient.cleanupTransport();
  48.             } catch (AxisFault e) {
  49.             }
  50.         }
  51.     }
  52. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

盛世宏图

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表