一、添加依赖
<!-- SpringBoot 2.4 以下版本-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId>
</dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.12</version>
</dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.1.12</version>
</dependency>
<!-- SpringBoot 2.5 及以上版本-->
<!--webservice start-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.4.3</version><!--<version>3.5.5</version>-->
</dependency>
<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>3.4.3</version>
</dependency>
<!--webservice end-->
二、提供webservice接口
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;@WebService(//name = "IHospitalizationDiagnosisService",targetNamespace = "http://webservice.core.service.test.com")
public interface IHospitalizationDiagnosisService {@WebMethod(operationName = "testDiagnose")String testDiagnose(@WebParam(name = "id") String id,@WebParam(name = "jsonString") String jsonString);}
三、提供实现类
@Slf4j
@Component
@WebService(targetNamespace = "http://webservice.core.service.test.com",endpointInterface = "com.test.service.core.webservice.IHospitalizationDiagnosisService")
public class HospitalizationDiagnosisServiceImpl implements IHospitalizationDiagnosisService {@Resourceprivate InpInfoMapper inpInfoMapper;@Overridepublic String testDiagnose(String id, String jsonString) {//业务逻辑return “”;}
四、WebServiceConfig配置类
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.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.annotation.Resource;
import javax.xml.ws.Endpoint;@Configuration
public class WebServiceConfig {@Resourceprivate IHospitalizationDiagnosisService iHospitalizationDiagnosisService;/*** 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问* 此方法被注释后, 即不改变前缀名(默认是services), wsdl访问地址为 http://127.0.0.1:8080/services/ws/api?wsdl* 去掉注释后wsdl访问地址为:http://127.0.0.1:8080/soap/ws/api?wsdl* http://127.0.0.1:8080/soap/列出服务列表 或 http://127.0.0.1:8080/soap/ws/api?wsdl 查看实际的服务* 新建Servlet记得需要在启动类添加注解:@ServletComponentScan** 如果启动时出现错误:not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet* 可能是springboot与cfx版本不兼容。* 同时在spring boot2.0.6之后的版本与xcf集成,不需要在定义以下方法,直接在application.properties配置文件中添加:* cxf.path=/service(默认是services)*/@Beanpublic ServletRegistrationBean disServlet() {return new ServletRegistrationBean(new CXFServlet(), "/IHospitalizationDiagnosisService/*");}/*** Apache CXF 核心架构是以BUS为核心,整合其他组件。* Bus是CXF的主干, 为共享资源提供一个可配置的场所,作用类似于Spring的ApplicationContext,这些共享资源包括* WSDl管理器、绑定工厂等。通过对BUS进行扩展,可以方便地容纳自己的资源,或者替换现有的资源。默认Bus实现基于Spring架构,* 通过依赖注入,在运行时将组件串联起来。BusFactory负责Bus的创建。默认的BusFactory是SpringBusFactory,对应于默认* 的Bus实现。在构造过程中,SpringBusFactory会搜索META-INF/cxf(包含在 CXF 的jar中)下的所有bean配置文件。* 根据这些配置文件构建一个ApplicationContext。开发者也可以提供自己的配置文件来定制Bus。*/@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {return new SpringBus();}@Beanpublic Endpoint endpoint() {EndpointImpl endpoint = new EndpointImpl(springBus(), iHospitalizationDiagnosisService);endpoint.publish("/EmrInpService");return endpoint;}
}
五、启动项目
页面访问:出现如图为成功
http://127.0.0.1:boot服务端口/boot-test/IHospitalizationDiagnosisService/EmrInpService?wsdl