解决SpringBoot集成Apache CXF 发布服务端后,客户端调用不了的问题
先说解决方法:
在META-INF中创建services文件夹, 以Provider这个类的全名来新建立一个文件,文件中的内容为指定实现类的全名,如下:
问题背景:
项目涉及到周边老系统的数据交互,采用webService的方式。调用webService有很多方式,我采用了最偷懒的方式–idea生成客户端代码(就此埋下了坑)。
后来需求增加功能,需要发布一个WebService服务接收周边系统下发的数据。(周边系统老的一批)。因为是SpringBoot项目,并且SpringBoot对Apache CXF也有很好的支持,所以我选择了cxf。架构大概如下:
项目架构:
-
Springboot 2.1.1.RELEASE
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>+ <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
-
cxf依赖
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.4</version> </dependency>
-
配置类
/** * @MathodName * @Description * @Author jiangying * @Params * @Returns * @Data 2020/3/18 */ @Configuration public class CxfConfig { //webService接口类 @Autowired private CertinoticeService certinoticeService; //webService接口类 @Autowired private UnderwriteService underwriteService; @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { SpringBus springBus = new SpringBus(); return springBus; } @Bean public Endpoint certEndpoint(){ EndpointImpl certEndpoint = new EndpointImpl(springBus(), certinoticeService); //服务路径(http://xxx.com/services/CertinoticeService) certEndpoint.publish("/CertinoticeService"); return certEndpoint; } @Bean public Endpoint anotherEndpoint(){ EndpointImpl underEndpoint = new EndpointImpl(springBus(), underwriteService); //服务路径 underEndpoint.publish("/UnderwriteService"); return underEndpoint; } }
启动正常访问,完美(才怪)
服务端访问是没有问题,可是突然发现自己原来的客户端都访问不了了:
javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionException
at org.apache.cxf.jaxws.ServiceImpl.getPort(ServiceImpl.java:360)
at org.apache.cxf.jaxws.ServiceImpl.getPort(ServiceImpl.java:349)
at javax.xml.ws.Service.getPort(Service.java:119)
at com.ccic.salesapp.customer.outService.esb.ecif.custCarInfoSearch.intf.CustCarInfoSearchService.getCustCarInfoSearchEndpoint(CustCarInfoSearchService.java:75)
at com.ccic.salesapp.customer.utils.WebServiceUtils.doCustCarInfoSearchRequest(WebServiceUtils.java:304)
at com.ccic.salesapp.customer.controller.CustomerServeController.test(CustomerServeController.java:45)
at com.ccic.salesapp.customer.controller.CustomerServeController$$FastClassBySpringCGLIB$$2ef4d2d9.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoi