spring boot应用调用webservice接口

您所在的位置:网站首页 springboot发布webservice接口 spring boot应用调用webservice接口

spring boot应用调用webservice接口

2023-08-14 23:15| 来源: 网络整理| 查看: 265

文章目录 概述一、Web Services是什么?二、使用步骤1.引入依赖2.创建配置类,根据接口路径生成Client 交给Spring 管理3.测试Web Services接口调用4.排错 总结

概述

在早期的系统中,通过 Web Services 实现服务,提供跨平台、跨语言的远程调用。最近在做一个火灾预警项目,需要拿到多个子系统的数据进行联动灭火设备,因为这些系统年代比较久远,只能通过子系统提供的 Web Services 接口来接入数据,让我们的火灾预警系统进行调用。

一、Web Services是什么?

Web Services 是应用程序组件 Web Services 使用开放协议进行通信 Web Services 是独立的(self-contained)并可自我描述 Web Services 可通过使用 UDDI 来发现 Web Services 可被其他应用程序使用 XML 是 Web Services 的基础

可以这样理解,以前的系统通过 Web Services 对接数据。现在的系统使用 RESTful 接口 + JSON数据格式 对接数据,技术在不断的更新迭代,但是使用技术的目的从未改变,那就是对接数据。

二、使用步骤 1.引入依赖

代码如下(示例):

org.springframework.boot spring-boot-starter-web-services org.apache.cxf cxf-spring-boot-starter-jaxws 3.2.1 org.apache.cxf cxf-rt-transports-http 3.2.1 2.创建配置类,根据接口路径生成Client 交给Spring 管理

可供测试的 web service 接口 :测试接口集合 这里以 腾讯 QQ 在线状态 WEB 服务 为例 http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl 通过输入 QQ 号码(String)检测 QQ 在线状态。返回数据(String)Y = 在线;N = 离线 ;E = QQ 号码错

首先,创建接口的client。提供接口的一端是 server,我们作为请求方是 client

/** * @author zhaoyuqi start * @create 2022-11-25 - 17:19 */ @Configuration @Slf4j public class JaxWsClientConfig { @Bean("JaxWsClient") public Client client() { // 创建动态客户端 JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance(); log.info("publicsecurity webService url : {}", "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"); //根据WebServices接口地址创建client Client client = clientFactory.createClient("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl"); HTTPConduit conduit = (HTTPConduit) client.getConduit(); HTTPClientPolicy policy = new HTTPClientPolicy(); policy.setAllowChunking(false); // 连接服务器超时时间 10秒 policy.setConnectionTimeout(10000); // 等待服务器响应超时时间 20秒 policy.setReceiveTimeout(20000); conduit.setClient(policy); return client; } } 3.测试Web Services接口调用

创建本地方法 checkOnline(String qqNumber),验证QQ状态

/** * @author zhaoyuqi start * @create 2022-11-25 - 17:22 */ @Service @Slf4j public class WebServiceImpl implements Webservcies { //注入 @Autowired @Qualifier("JaxWsClient") private Client client; /** * 验证 QQ 号码的状态,这里以我自己的QQ为例 * @param qqNumber */ @Override public void checkOnline(String qqNumber) { try { //invoke(接口中的方法名称,方法的参数) Object[] objects = client.invoke("qqCheckOnline", qqNumber); if (objects != null && objects.length > 0) { String isOnlineStr = (String) objects[0]; log.info(qqNumber + " is {}", isOnlineStr.equals("Y") ? "在线" : "离线"); } } catch (Exception e) { log.error("抛出了异常:{}" + e.getMessage()); } } }

测试类

@SpringBootTest class BootWebServiceApplicationTests { @Autowired private Webservcies webservcies; @Test void contextLoads() { webservcies.checkOnline("854296521"); } }

得到结果: 在这里插入图片描述

4.排错

在这里插入图片描述 原因: 搭建SpringBoot中的验证数据机制时出现的错误 对于SpringBoot新版本现在不会自动导入校验机制,需要我们手动导入。 此句话Add a provider like Hibernate Validator (RI) to your classpath. 推测导入与Hibernate 有关的包,因此导入依赖

org.hibernate hibernate-validator 6.1.3.Final 总结

实现了springboot 应用 和 web services 通信



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3