引入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方法可用.以上,服务便搭建好了.