• SpringBoot使用webservice


    Pom.xml

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
            <relativePath/>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
                <version>3.1.12</version>
            </dependency>
            <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>2.6</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.30</version>
            </dependency>
        </dependencies>

    服务接口

    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    
    /**
     * 网吧web services 接口
     * @author xiaojf 2017/7/24 21:35
     */
    @WebService(targetNamespace = "http://service.netbar.temple.xiaojf.cn")// 命名空间,一般是接口的包名倒序
    public interface NetbarServices {
        @WebMethod
        String sayHello(@WebParam(name = "userName") String name);
    }

    服务实现

    import org.springframework.stereotype.Component;
    
    import javax.jws.WebService;
    
    /**
     *  网吧web services 接口实现
     * @author xiaojf 2017/7/24 21:38
     */
    @WebService(serviceName = "NetbarServices"//服务名
            ,targetNamespace = "http://service.netbar.temple.xiaojf.cn"//报名倒叙,并且和接口定义保持一致
            ,endpointInterface = "cn.xiaojf.temple.netbar.service.NetbarServices")//包名
    @Component
    public class NetbarServicesImpl implements NetbarServices {
        @Override
        public String sayHello(String name) {
            return "hello , "+ name;
        }
    }

    发布服务

    import org.apache.cxf.Bus;
    import org.apache.cxf.jaxws.EndpointImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.xml.ws.Endpoint;
    
    @Configuration
    public class CxfConfig {
        @Autowired
        private Bus bus;
        @Autowired
        private NetbarServices netbarServices;
    
        @Bean
        public Endpoint endpoint() {
            EndpointImpl endpoint = new EndpointImpl(bus,netbarServices);
            endpoint.publish("/testservice");//接口发布在 /test 目录下
            return endpoint;
        }
    }

    验证

    http://localhost:8002/services/mytestservice?wsdl

    创建成功以后webservice是在 /services/目录下

    如http://localhost:8080/services/mdmws?wsdl

  • 相关阅读:
    【阿里云IoT+YF3300】6.物联网设备报警配置
    【阿里云IoT+YF3300】5. Alink物模型之服务下发
    【阿里云IoT+YF3300】4.Alink物模型之事件触发
    【阿里云IoT+YF3300】3. Alink物模型之属性上传和下发
    腾讯物联TencentOS tiny上云初探
    【阿里云IoT+YF3300】2.阿里云IoT云端通信Alink协议介绍
    【阿里云IoT+YF3300】1.时代大背景下的阿里云IoT物联网的现状和未来
    阿里云的物联网之路
    阿里云智能接入网关体验
    imx:MfgTool
  • 原文地址:https://www.cnblogs.com/liuxm2017/p/9810805.html
Copyright © 2020-2023  润新知