• springboot整合搭建webservice项目


    1 背景

    本文章是记录是springboot整合webservice,至于webservice的介绍就不过多赘述。webservice分为客户端和服务端,使用场景不是很多,但大部分是对接第三方需要用到,因为对接他们必须按照第三方的规则走(迫不得已),作为服务端使用场景少之又少。

    全部代码地址:https://gitee.com/yhyocean/webservice

    2 项目搭建

    生成springboot项目后,增加以下操作。

    2.1 pom依赖

    2.2核心依赖项

     		<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- webService-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web-services</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
                <version>3.2.1</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.3 maven生成客户端代码插件

                <plugin>
                    <groupId>com.helger.maven</groupId>
                    <artifactId>jaxws-maven-plugin</artifactId>
                    <version>2.6.2</version>
                    <configuration>
                        <!--生成客户端代码的包路径-->
                        <packageName>
                            com.webservice.consumer.business
                        </packageName>
                        <wsdlUrls>
                            <!--服务端提供的接口地址-->
    <!--                        <wsdlUrl>http://127.0.0.1:8080/services/ws/api?wsdl</wsdlUrl>-->
                            <wsdlUrl>http://soa.joyoung.com/gateway/export/BusinessCenterService?wsdl</wsdlUrl>
                        </wsdlUrls>
                        <wsdlFiles>
    <!--                        <wsdlFile>-->
    <!--                            /Users/workspace/custom/webservice/src/main/java/com/webservice/consumer-->
    <!--                        </wsdlFile>-->
                        </wsdlFiles>
                        <keep>true</keep>
                        <verbose>true</verbose>
                        <sourceDestDir>src\main\java</sourceDestDir>
                        <vmArgs>
                            <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg>
                        </vmArgs>
                    </configuration>
                </plugin>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    2.4 增加配置项

    package com.webservice.config;
    
    import com.webservice.consumer.business.BusinessCenterService;
    import com.webservice.consumer.demo.ServiceDemoImplService;
    import com.webservice.provider.demo.ServiceDemo;
    import org.apache.cxf.Bus;
    import org.apache.cxf.bus.spring.SpringBus;
    
    import org.apache.cxf.jaxws.EndpointImpl;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.annotation.Resource;
    import javax.xml.ws.Endpoint;
    
    /**
     * WebServiceConfig
     *
     * webservice配置类
     *
     **/
    @Configuration
    public class WebServiceConfiguration {
    
        /**
         * webservice服务端路径
         */
        private final static String WEBSERVICE_PATH = "/ws/api";
    
        @Resource
        private ServiceDemo serviceDemo;
    
        @Bean(name = Bus.DEFAULT_BUS_ID)
        public SpringBus springBus(){
            return new SpringBus();
        }
    
        @Bean
        public Endpoint endpoint(){
            EndpointImpl endpoint = new EndpointImpl(springBus(), serviceDemo);
            endpoint.publish(WEBSERVICE_PATH);
            return endpoint;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    3 发布服务端

    3.1 服务端接口

    @WebService(name = "TestServiceDemo",targetNamespace = "http://service.webservice.com")
    public interface ServiceDemo {
    
        /**
         * 服务端提供接口定义
         * @param data
         * @return
         */
        @WebMethod
        String test(@WebParam String data);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    3.2 服务端接口实现

    @Slf4j
    @Component
    @WebService(name = "TestServiceDemo",targetNamespace = "http://service.webservice.com",
    endpointInterface = "com.webservice.provider.demo.ServiceDemo")
    public class ServiceDemoImpl implements ServiceDemo {
    
        /**
         * 服务端提供接口定义实现
         * @param param
         * @return
         */
        @Override
        public String test(String param) {
            log.info("test webservice receive {}",param);
            param = "hello "+param;
            return param;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    3.3 增加配置项

        @Bean
        public ServiceDemoImplService serviceDemoImplService(){
            return new ServiceDemoImplService();
        }
    
    • 1
    • 2
    • 3
    • 4

    3.4 启动

    springboot启动后访问
    http://127.0.0.1:8080/services/ws/api?wsdl
    这也就是对外提供接口地址。
    在这里插入图片描述

    4 增加客户端

    4.1 生成客户端代码

    根据服务端提供的接口,生成客户端代码方式很多种,我选择较为方便的方式,就是maven插件,如 2.3
    在这里插入图片描述

    4.2 单元测试

        public static void t1(){
            ServiceDemoImplService serviceDemoImplService = new ServiceDemoImplService();
            String test = serviceDemoImplService.getTestServiceDemoPort().test("1111");
            System.out.println(test);
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    5 总结

    webservice分为客户端可服务端。
    服务端顾名思义就是对外提供服务接口的。
    而客户端是调用服务端提供的服务的,是根据服务端提供的服务和定义生成客户端代码,然后调用服务端的接口,而具体底层调用处理我们无须关注这些代码,因为将有插件生成这些代码。

    6 全部实现代码

    仓库地址:https://gitee.com/yhyocean/webservice

  • 相关阅读:
    解决报错:fatal: Authentication failed for ‘https://github.com/*/*.git/‘
    电力巡检/电力抢修行业解决方案:AI+视频技术助力解决巡检监管难题
    计算机网络笔记汇总链接
    1300*B. Sort the Array(排序&构造)
    机器学习——异常检测
    SpringMVC中bean的加载控制
    Java集合
    SpringCloud系列——Ribbon day2-2
    BGP进阶:BGP 基础实验配置
    【数组及指针经典笔试题解析】
  • 原文地址:https://blog.csdn.net/weixin_43831997/article/details/127834152