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

posted @ 2018-10-18 15:32  liuxm-刘小明  阅读(3655)  评论(0编辑  收藏  举报