使用cxf实现的webservice在eclipse中运行时无问题,打包成jar后控制台运行报错(也是我上一篇文章里介绍的那个程序遗留下来的问题)
十月 30, 2020 3:12:42 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://test/}Dny from class test.Dny
Exception in thread "main" org.apache.cxf.service.factory.ServiceConstructionException: Could not resolve a binding for null
at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createBindingInfo(AbstractWSDLBasedEndpointFactory.java:361)
at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpointInfo(AbstractWSDLBasedEndpointFactory.java:268)
at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createEndpoint(AbstractWSDLBasedEndpointFactory.java:146)
at org.apache.cxf.frontend.ServerFactoryBean.create(ServerFactoryBean.java:168)
at test.Main.main(Main.java:11)
Caused by: org.apache.cxf.BusException: No binding factory for namespace http://schemas.xmlsoap.org/soap/ registered.
at org.apache.cxf.bus.managers.BindingFactoryManagerImpl.getBindingFactory(BindingFactoryManagerImpl.java:93)
at org.apache.cxf.frontend.AbstractWSDLBasedEndpointFactory.createBindingInfo(AbstractWSDLBasedEndpointFactory.java:348)
... 4 more
上网搜索了下相关帖子,有几个解决方案,然而我这程序就是一个很普通的java程序,java -jar test.jar执行,除了cxf没有用任何框架,没有springboot,也不是java web,更不存在pom.xml这种maven程序才有的东西,所以没办法照着网上的做法在xml配置文件里修改。
不过看了几篇帖子也是有启发的,有个解决方案是maven程序在pom.xml里改编译器,恰好我之前接触过这种解决方案,虽然没用过,我先前写过的帖子里,Elasticsearch程序也是这样只有打包后才报错。
总之深思熟虑过后,我个人认为这次问题就是出在编译器里边,eclipse自带的编译功能会把同路径同名配置文件覆盖掉(或者说同路径同名文件只加载第一个出现的文件?之前Elasticsearch程序现在仔细想也可能就是这个原因),当程序里使用到相关配置时就报错了。
解决方法就是将所有冲突文件复制出来人工合并后再编译:
1.打开cxf-core-3.4.0.jar、cxf-rt-bindings-soap-3.4.0.jar、cxf-rt-transports-http-3.4.0.jar、cxf-rt-transports-http-jetty-3.4.0.jar、cxf-rt-wsdl-3.4.0.jar的META-INF\cxf\bus-extensions.txt文件。
2.在项目src下创建META-INF\cxf\bus-extensions.txt,将上边打开的文件内容都复制到这个文件里。
3.编译生成jar,打开该jar后可以发现该文件使用的是我合并后的文件。
问题解决。