spring boot应用调用webservice接口


概述

在早期的系统中,通过 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 管理


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

3.测试Web Services接口调用

测试类

@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 通信

经验分享 程序员 微信小程序 职场和发展