• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

spring boot应用调用webservice接口

武飞扬头像
独欢、
帮助2


概述

在早期的系统中,通过 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.引入依赖

代码如下(示例):

<!-- webService-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>

        <!-- CXF webservice -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.1</version>
        </dependency>
学新通

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 有关的包,因此导入依赖

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.1.3.Final</version>
        </dependency>

总结

实现了springboot 应用 和 web services 通信

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhckehei
系列文章
更多 icon
同类精品
更多 icon
继续加载