【1/26】Java-WebService客户端示例

527 阅读4分钟

我正在参加「掘金·启航计划」

image.png

前言

下文时间地点均为虚构,如有雷同对不起!

1922年除夕前夕盛京暴雪,左羊正在进行一个与甲方爸爸的丙方弟弟对接API的任务,期间遇到一个通过WebService接近实时获取数据的功能比较有趣的,所以左羊想在这里记录下。好了,闲言少叙,不重要的话少说。

可用于测试的接口

首先我准备使用webxml:www.webxml.com.cn/ 的天气预报查询服务作为获取数据进行测试的接口。目的是使用Java代码通过输入省份名称获取目标名称省份下的城市名称及城市代码。

WSDL地址为:ws.webxml.com.cn/WebServices…

接口地址为:ws.webxml.com.cn/WebServices…

接口名称为:getSupportCityString

参数名为:theRegionCode

参数值为:青海

返回值为:青海省下属的城市名称及城市代码

image.png  

WebService 接口测试工具

上图为SoapUi的测试内容。

SoapUi的下载地址为www.soapui.org/downloads/ ,安装完成后打开SoapUi。

1、【File】-->【New SOAP Project】

image.png

2、在【Inital WSDL】处输入WSDL地址,即可解析出该WSDL地址所有的WebService接口地址。

image.png

3、点击咱们需要使用接口的Request,例如getSupportCitySring接口

image.png

4、咱们在【theRegionCode】位置的?处输出“青海”然后点击绿色的执行箭头即可获取青海省下属的城市名称及城市代码。

image.png

三方执行器/WebService客户端

左羊在网上搜索了大量WebService客户端,最终选用了HuTool的SOPA客户端来执行WebService请求。下面左羊引用HuTool的一段话介绍SOAP客户端。

在接口对接当中,WebService接口占有着很大份额,而我们为了使用这些接口,不得不引入类似Axis等库来实现接口请求。

现在有了Hutool,就可以在无任何依赖的情况下,实现简便的WebService请求。

 

测试用例

1、首先创建一个SpringBoot项目,然后在其pom.xml中配置HuTool的依赖,如下图。

image.png

2、编写WebService示例代码,创建TestClient测试类。

image.png

根据上图我们得知:

接口地址为:ws.webxml.com.cn/WebServices…

接口名称为:getSupportCityString

参数名为:theRegionCode

参数值为:青海

 

示例代码

代码为

image.png

package com.zuoyang.web_service_client;
import cn.hutool.core.lang.Console;
import cn.hutool.http.webservice.SoapClient;
public class TestClient {
    public static void main(String[] args) {
        // 创建SOAP客户端
        SoapClient client = SoapClient.create("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx");
        // 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间 .setMethod("web:getSupportCityString", "http://WebXml.com.cn/")
        client.setMethod("web:getSupportCityString", "http://WebXml.com.cn/");
        // 设置参数,此处自动添加方法的前缀:web .setParam("theRegionCode", "青海);
        client.setParam("theRegionCode", "青海");
        // 发送请求,参数true表示返回一个格式化后的XML内容
        // 返回内容为XML字符串,可以配合XmlUtil解析这个响应
        Console.log(client.send(true));
    }
}

执行结果为

image.png

扩展

上面我简单的介绍了单个String类型入参的用法,但是在现实成产过程中咱们可能会有多参数的,参数为无定长数组的等等各种各样咱们该咋弄?莫慌既然左羊能说出这些情况,那我一定是趟过上述的坑。

咱们百度下大概都能WebSerivce基本上是基于XML协议实现,所以上述的各种不过是XML中的一个节点罢了。我们执行javax.xml.soap下的XML对象方法和SOAP的getMethodEle().addChildElement();方法即可处理上述问题,咱们来看下代码。

image.png

 

package com.zuoyang.web_service_client;
import cn.hutool.core.lang.Console;
import cn.hutool.http.webservice.SoapClient;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;
public class DemoClient {
    public static void main(String[] args) throws SOAPException {
        // 新建客户端
        SoapClient client = SoapClient.create("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx");
        // 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间 .setMethod("web:getSupportCityString", "http://WebXml.com.cn/")
        client.setMethod("web:getSupportCityString", "http://WebXml.com.cn/");
        // 设置参数,此处自动添加方法的前缀:web .setParam("theRegionCode", "青海);
        client.getMethodEle().addChildElement(getPram("辽宁"));
        // 发送请求,参数true表示返回一个格式化后的XML内容
        // 返回内容为XML字符串,可以配合XmlUtil解析这个响应
        Console.log(client.send(true));
    }
    public static SOAPElement getPram(String name) throws SOAPException {
        //实例化SOAP工厂
        SOAPFactory soapfactory = SOAPFactory.newInstance();
        //通过soap工厂创建标签 <web:theRegionCode>?</web:theRegionCode>
        SOAPElement theRegionCode = soapfactory.createElement("theRegionCode", "web", "http://WebXml.com.cn/");
        //将name中的内容保存到创建的theRegionCode标签中
        theRegionCode.setTextContent(name);
        return theRegionCode;
    }


}

运行结果

image.png

后面大家可以根据XML的结构体去创建自己需要的SOAP结构体来服务于生产。

结语

目前左羊掌握的SOAP就都呈现给大家了,感谢大家的观看Yes!