Spring boot 2.x基于cxf发布webservice服务

一. pom.xml添加相关依赖

<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
    <version>3.2.5</version>
</dependency>

二 开发WebService接口类和接口对应的实现类

添加interface

package com.dhcens.platform.service;


import org.springframework.stereotype.Component;

import javax.jws.WebMethod;
import javax.jws.WebService;

/**
 * @author zhibao
 */
@WebService(name = "QueryWebService", // 暴露服务名称
        targetNamespace = "http://dhcens.com.cn/"// 命名空间,一般是接口的包名倒序
)
@Component
public interface QueryDataService {
    @WebMethod
    public String queryData(String paramters);

}

添加实现类

package com.dhcens.platform.service.impl;

import com.dhcens.platform.service.QueryDataService;
import org.springframework.stereotype.Service;

import javax.jws.WebService;

/**
 *
 * @author zhibao
 */
@WebService(name = "QueryWebService", // 暴露服务名称
        targetNamespace = "http://dhcens.com.cn/"// 命名空间,一般是接口的包名倒序
)
@Service
public class QueryDataServiceImpl implements QueryDataService {
    @Override
    public String queryData(String paramters) {
        return "this is a webservice";
    }
}

三 Webservice服务发布配置

发布服务对外提供接口

package com.dhcens.platform.configuration;


import com.dhcens.platform.service.QueryDataService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;
@Configuration
public class WebServiceConfig {

    @Bean(name = "cxfServlet")
    public ServletRegistrationBean dispatcherServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
    }
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }
    @Autowired
    public QueryDataService queryDataService;

    @Bean
    public Endpoint queryDataService() {
        EndpointImpl endpoint = new EndpointImpl(springBus(),queryDataService);
        endpoint.publish("/QueryWebService");
        return endpoint;
    }
}

四 服务调试

测试接口

浏览器打开服务地址http://127.0.0.1:9092/platform/ws/QueryWebService?wsdl

 如果是这样就没问题,我们用SoapUI测试下我们得接口

 关于方法参数名称得指定只需要在接口类中添加

@WebParam(name="XXX")

一个简单的WebService服务就搭建完成了

猜你喜欢

转载自www.cnblogs.com/backback/p/12187835.html
今日推荐