java 调用wsdl协议接口简单实用方法最新推荐
作者:锋丷
文章介绍了如何使用POM导入依赖,并编写一个测试类来调用不同的Web服务接口,通过访问接口地址,我们可以获取请求和返回的body,并进一步解析返回的JSON结果,感兴趣的朋友一起看看吧
1.导入pom
<!-- JAX-WS API --> <dependency> <groupId>javax.xml.ws</groupId> <artifactId>jaxws-api</artifactId> <version>2.3.1</version> </dependency> <!-- JAX-WS Runtime --> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> <version>2.3.1</version> </dependency> <!-- 工具集 --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.15</version> </dependency>
2.编写测试类TestWsdl
import cn.hutool.http.webservice.SoapClient; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import javax.xml.soap.SOAPMessage; /** * 测试调用wsdl接口 * @author laijiangfeng * @date 2024/9/27 9:51 */ public class TestWsdl { public static void main(String[] args) throws Exception { // 创建SoapClient实例 wsdl协议请求地址:http://xxx.cn/services/GanSuInterface (去掉?wsdl) SoapClient client = SoapClient.create("http://xxx.cn/services/GanSuInterface") // 设置方法名和命名空间(命名空间url:http://webservice.xxx.xxx ,方法名:getSydwJsonList) // tns 命名空间targetNamespace,没有命名空间时直接写方法名即可 .setMethod("tns:getSydwJsonList", "http://webservice.xxx.xxx") // 设置参数(最后的false参数表示参数不加命名空间的前缀web) .setParam("in0", "afe9cff39173ff8901917a54c90e448d", false); //获取SOAPMessage实例(此步作用是输出请求的XML参数,实际开发并不需要) SOAPMessage message = client.getMessage(); System.out.println(client.getMsgStr(true)); // 发送请求(true表示输出的结果格式化处理) String send = client.send(true); Document document = DocumentHelper.parseText(send); Element root = document.getRootElement(); String value = root.getStringValue(); System.out.println(send); System.out.println(value); } }
命名空间url不同的接口可能不一样自己通过访问接口地址获取
3.运行main结果
上面第一部分是请求body、第二部分是返回的结果body、第三部分是解析后的结果json
到此这篇关于java 调用wsdl协议接口简单实用方法最新推荐的文章就介绍到这了,更多相关java 调用wsdl接口内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!