1、前言
相信很多人在项目开发对接过程中,有好多老项目的接口还是webservice接口,就导致调用这些接口时需要封装调用WebService接口工具类,现在有了Hutool工具库,只需要引入Hutool就可以完成对WebService接口的调用,方便很多,只需几行代码,省事不少,下面是在一个项目中的使用记录一下。
2、WebService相关概念
调用方法名
调用方法名,在WebService暴露的wsdl文档中给出,配合命名空间使用调用接口
命名空间
规范WebServicee接口,方便调用者调用。
参数
参数就是,调用该接口需要提供的参数。
3、实战
pom依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
yml配置WebService接口相关信息
#webservice
ws:
#接口路径
url: http://111.11.82.93:13001/jyyWebS.asmx
#命名空间
namespace: http://tempuri.org/
#审批取消方法名
approveCancel: delprocess
WebService接口相关信息配置类
package com.example.springbootdemo.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* <p>
*
* </p>
*
* @author yurenwei
* @since 2023/9/6
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "ws")
public class WebserviceConfig {
/**
* 接口地址
*/
private String url;
/**
* 命名空间
*/
private String namespace;
/**
* 审批取消方法
*/
private String approveCancel;
}
通过hutool工具类调用webservice接口代码
package com.example.springbootdemo.sevice;
import cn.hutool.http.webservice.SoapClient;
import com.example.springbootdemo.config.WebserviceConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* <p>
* webservice接口
* </p>
*
* @author yurenwei
* @since 2023/9/6
*/
@Slf4j
@Service
public class WebserviceService {
@Autowired
private WebserviceConfig webserviceConfig;
public void test() {
Map<String, Object> map = new HashMap<>();
map.put("billid",1);
map.put("billtype","62");
map.put("buttflag","newzcpt");
map.put("enforced","");
// 新建客户端
SoapClient client = SoapClient.create(webserviceConfig.getUrl())
// 设置要请求的方法和对应的命名空间
.setMethod(webserviceConfig.getApproveCancel(), webserviceConfig.getNamespace())
// 设置参数
.setParams(map, false);
// 发送请求
String result = client.send();
log.info("result:{}",result);
}
}
4、测试
启动项目
通过接口调用webservice
看日志信息,请求通了,只是数据不存在。
至此,通过Hutool工具类调用WebService接口实践结束了。