SpringBoot整合CXF,实现WebService对外接口服务

引入Apache的CXF依赖

        <!-- apache-cxf -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.1.6</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>3.1.6</version>
		</dependency>

WebService服务的创建

1.创建一个Interface--名为"GwWebService"

 2.创建一个Config,用于配置GwWebService服务--名为"WebServiceConfig"

 WebServiceConfig代码如下:

package cn.gdxiash.springbootwebservicedemo.config;

import cn.gdxiash.springbootwebservicedemo.webservice.GwWebService;
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;

/**
 * @author ShangHai
 * @date 2020/09/16 9:03
 * @desc
 */
@Configuration
public class WebServiceConfig {

    @Autowired
    private GwWebService gwWebService;

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    /**
     * 配置CXFServlet的拦截路径
     * @return
     */
    @Bean(name = "cxfServlet")
    public ServletRegistrationBean cxfServlet(){
        return new ServletRegistrationBean(new CXFServlet(),"/webservice/*");
    }

    @Bean(name = "GwWebServiceEndpoint")
    public Endpoint endpoint() {
        //将GwWebService注册到WebService服务中
        EndpointImpl endpoint = new EndpointImpl(springBus(), gwWebService);
        //指定GwWebService服务匹配路径为/gw
        endpoint.publish("/gw");
        return endpoint;
    }

}

3.创建GwWebService接口实现子类并配置为一个WebService服务--名为"GwWebServiceImpl"

4.创建一个WebService调用方法,在接口中需要添加@WebMethod注解,然后子类重写即可.

5.服务方法实现后,即可启动SpringBoot项目.启动成功后,项目占用8080端口.

6.为了验证此服务可用,需要在浏览器上输入http://localhost:8080/webservice/gw?wsdl查看服务信息

7.访问后出现了WebService接口的服务地址、可供调用的方法,说明WebService服务已经成功搭建起来了.


使用SoapUI测试GwWebService服务

 1.打开SoapUI接口测试工具,创建一个空项目.

2.创建一个wsdl,建立好与GwWebService服务的连接.

3.随意输入一个形参值,点击发送请求进行接口的测试.

4.SoapUI响应窗口正常得到了接口设置的json值,说明此GwWebService服务的getProductById方法可用.以上,服务便搭建好了.

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值