Hutool工具库调用webservice接口

原因

在项目对接过程中,有好多老项目的接口还是webservice接口,就导致需要使用这些接口是还要映入Axis库,现在有hutool工具库的存在,只需要引入hutool就可以完成对webservice接口的调用,下面是在一个项目中的使用记录一下

1. 引入Hutool依赖

<dependency>
   <groupId>cn.hutool</groupId>
   <artifactId>hutool-all</artifactId>
   <version>5.7.4</version>
 </dependency>

2. 使用

示例1

xml数据

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:getCountryCityByIp>
         <!--Optional:-->
         <arg0>192.168.0.2</arg0>
      </web:getCountryCityByIp>
   </soapenv:Body>
</soapenv:Envelope>

代码构建soap请求

private static void sendWebservice() throws SOAPException {
        String authenticationUrl = "http://xxx?wsdl";
        QName qname = new QName("RequestSOAPHeader");
        SoapClient soapClient = SoapClient.create(authenticationUrl)
               .setMethod("web:getCountryCityByIp","http://webservice.com/")
               .setParam("arg0","192.168.0.2",false);
        //设置请求头参数       
        SOAPHeaderElement soapHeaderElement = soapClient.addSOAPHeader(qname);
        SOAPElement user = soapHeaderElement.addChildElement("user");
        //账号
        user.setTextContent("oa");
        SOAPElement password = soapHeaderElement.addChildElement("password");
        //密码
        password.setTextContent("bdf01990300ed0d0a306428");
        //发送请求
        String result = soapClient.send(true);
        System.out.println(result);
        Document document = XmlUtil.parseXml(result);
        NodeList aReturn = document.getElementsByTagName("return");
        for (int i = 0; i < aReturn.getLength(); i++) {
            Node node = aReturn.item(i);
            Map<String, Object> map = XmlUtil.xmlToMap(node);
            System.out.println(map);
        }
    }
示例2

xml数据增加SOAPHeader

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.com/">
   <soapenv:Header/>
   <RequestSOAPHeader>
   <user>zs</user>
   </RequestSOAPHeader> 
   <soapenv:Body>
      <web:getCountryCityByIp>
         <!--Optional:-->
         <arg0>192.168.0.2</arg0>
      </web:getCountryCityByIp>
   </soapenv:Body>
</soapenv:Envelope>
private static void sendWebservice() throws SOAPException {
        String authenticationUrl = "http://xxx?wsdl";
        QName qname = new QName("http://sys.webservice.client/","RequestSOAPHeader");
        SoapClient soapClient = SoapClient.create(authenticationUrl)
               .setMethod("web:getCountryCityByIp","http://webservice.com/")
               .setParam("arg0","192.168.0.2",false)
        SOAPHeaderElement soapHeaderElement = soapClient.addSOAPHeader(qname);
        SOAPElement user = soapHeaderElement.addChildElement("user");
        //账号
        user.setTextContent("zs");
        String result = soapClient.send(true);
        System.out.println(result);
        Document document = XmlUtil.parseXml(result);
        NodeList aReturn = document.getElementsByTagName("return");
        for (int i = 0; i < aReturn.getLength(); i++) {
            Node node = aReturn.item(i);
            Map<String, Object> map = XmlUtil.xmlToMap(node);
            System.out.println(map);
        }
    }
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hutool是一个Java工具,它提供了许多简化开发的工具方法和类。其中包括了对Web服务调用的支持,包括了对Webservice服务的调用Hutool提供了一个`SoapClient`类,可以用来调用Webservice接口。引用中给出了一个超简单的Webservice调用的示例,你只需要输入Webservice接口名和接口参数即可进行接口调用。 首先,你需要下载一个叫做SoapUI的工具用来测试Webservice接口。接下来,你需要获取Webservice的URL、命名空间、方法名以及参数格式。在引用中提供了一个Webservice的URL示例,即http://www.webxml.com.*** 接下来,你可以使用Hutool的`SoapClient`类来进行Webservice接口调用。你可以使用`SoapClient.create()`方法创建一个`SoapClient`对象,并使用`setEndpoint`方法设置Webservice的URL。然后,使用`setSoapAction`方法设置命名空间和方法名,并使用`addParam`方法添加参数。最后,使用`execute`方法发送请求,并获取返回结果。 总结起来,使用Hutool调用Webservice接口的步骤如下: 1. 下载并安装SoapUI工具; 2. 获取Webservice的URL、命名空间、方法名和参数格式; 3. 使用Hutool的`SoapClient`类创建一个`SoapClient`对象,并设置Webservice的URL; 4. 使用`setSoapAction`方法设置命名空间和方法名,使用`addParam`方法添加参数; 5. 使用`execute`方法发送请求,并获取返回结果。 希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值