解决springboot整合cxf启动报错,原因是版本问题
springboot整合cxf启动报错
错误信息如下
[DEBUG] 2021-01-26 11:28:47,848 [main] org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - Application failed to start due to an exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' available: expected at least 1 bean which qualifies as autowire candidate. Dependency a  at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1658)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1217)
at org.springframework.beans.factory.solveDependency(DefaultListableBeanFactory.java:1171)
at org.springframework.beans.factory.solveAutowiredArgument(ConstructorResolver.java:857)
at org.springframework.beans.factory.ateArgumentArray(ConstructorResolver.java:760)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1341)
at org.springframework.beans.factory.ateBeanInstance(AbstractAutowireCapableBeanFactory.java:1187)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.ateBean(AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.Singleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.Bean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:392)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321)
at org.springframework.beans.factory.ateBeanInstance(AbstractAutowireCapableBeanFactory.java:1160)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.ateBean(AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.Singleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.Bean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.BeansOfType(DefaultListableBeanFactory.java:606)
at org.springframework.boot.web.Registrars(ErrorPageRegistrarBeanPostProcessor.java:76)
at org.springframework.boot.web.server.ErrorPageRegistrarBeanPostProcessor.postProcessBeforeInitialization(ErrorPageRegistrarBeanPostProcessor.java:67)
at org.springframework.boot.web.server.ErrorPageRegistrarBeanPostProcessor.postProcessBeforeInitialization(ErrorPageRegistrarBeanPostProcessor.java:56)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:414)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
at org.springframework.beans.factory.ateBean(AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.Singleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.Bean(AbstractBeanFactory.java:204)
at org.springframework.boot.WebServerFactory(ServletWebServerApplicationContext.java:210)
at org.springframework.boot.ateWebServer(ServletWebServerApplicationContext.java:179)
at org.springframework.boot.Refresh(ServletWebServerApplicationContext.java:153)
at t.fresh(AbstractApplicationContext.java:543)
at org.springframework.boot.fresh(ServletWebServerApplicationContext.java:141)
at org.springframework.fresh(SpringApplication.java:744)
at org.springframework.freshContext(SpringApplication.java:391)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204)
at com.cignacmb.ApiApplication.main(ApiApplication.java:13)
[ERROR] 2021-01-26 11:28:47,849 [main] org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter -
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 1 of constructor in org.springframework.boot.autoconfigure.ErrorMvcAutoConfiguration required a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' that could not be found.
The following candidates were found but could not be injected:
- Bean method 'dispatcherServletRegistration' in 'DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration' not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' in your configuration.
使⽤的SpringBoot版本是2.1.8,使⽤了cxf-spring-boot-starter-jaxws的版本是3.2.6。
⽹上有的说是版本不匹配造成的,但是我这边发现不是版本不匹配造成的,⽽是我的cxf配置类⾥注册Servlet的bean的name是dispatcherServlet,造成错误的。因此注册的servlet的bean的名称不能是dispatcherServlet
@Bean(name = "cxfServlet")
public ServletRegistrationBean cxfServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");
}
Springboot 整合 CXF 出现的bug总结
在之前整合cxf进spingboot项⽬时,总是报错Error creating bean with name 'endpoint' defined in clas
s path,经过打断点源码层层调试,最终发现springboot在整合cxf的时候⼀些特性,解决⽅法如下:
报错信息如下
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'endpoint' defined in class path resource [net/chinanets/sj/config/CxfConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.b  at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
at org.springframework.beans.factory.ateBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
at org.springframework.beans.factory.ateBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.Object(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.Singleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.Bean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at t.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at t.fresh(AbstractApplicationContext.java:543)
at org.t.fresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.fresh(SpringApplication.java:693)
at org.springframework.freshContext(SpringApplication.java:360)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
at net.chinanets.sj.SjApplication.main(SjApplication.java:22)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at flect.Method.invoke(Method.java:498)
at org.springframework.start.RestartLauncher.run(RestartLauncher.java:49)
1.服务端接⼝和接⼝的实现类,两者其⼀必须在@WebService注解后加⼊serviceName,targetNamespace,endpointInterface说明,且不能同时都加!源码中代码如下:
2.代码:
String sei = getImplementorClassName();
boolean seiFromWsAnnotation = true;
if (StringUtils.isEmpty(sei)) {
seiFromWsAnnotation = false;
sei = getWSInterfaceName(implementorClass);
spring boot是啥}
if (!StringUtils.isEmpty(sei)) {
try {
seiClass = ClassLoaderUtils.loadClass(sei, implementorClass);
} catch (ClassNotFoundException ex) {
throw new String("SEI_LOAD_FAILURE_MSG"), ex);
}
WebService seiAnnotation = Annotation(WebService.class);
if (null == seiAnnotation) {
throw new String("SEI_WITHOUT_WEBSERVICE_ANNOTATION_EXC"));
}
if (seiFromWsAnnotation
&& (!StringUtils.isEmpty(seiAnnotation.portName())
|| !StringUtils.isEmpty(seiAnnotation.serviceName())
|| !StringUtils.dpointInterface()))) {
String expString = String("ILLEGAL_ATTRIBUTE_IN_SEI_ANNOTATION_EXC");
throw new WebServiceException(expString);
}
其中seiAnnotation指的是实现类的serviceName,targetNamespace,endpointInterface,其为空,则不会抛此异常。
3.解决这个异常后,⼜抛出另⼀个异常,怀疑是jar包问题,cmd进⼊⼯作空间项⽬⽬录,使⽤ mvn dependency:tree 查看项⽬jar包结构,发现cxf⾃动带⼊XmlSchema解析包,但是项⽬中⼜有⼀个单独
的XmlSchema解析包,导致冲突,删除单独的xml解析包,即可解决。
4.springboot 可直接在application.properties配置⽂件中配置接⼝访问的过滤路径。cxf.path=/soap 可以替换 CxfConfig 配置类中的
@Bean
public ServletRegistrationBean dispatcherServlet() {
return new ServletRegistrationBean(new CXFServlet(), "/soap/*");
}
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。