Springboot 整合 CXF 出现的bug总结

在之前整合cxf进spingboot项目时,总是报错Error creating bean with name 'endpoint' defined in class 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.beans.BeanInstantiationException: Failed to instantiate [javax.xml.ws.Endpoint]: Factory method 'endpoint' threw exception; nested exception is javax.xml.ws.WebServiceException: javax.xml.ws.WebServiceException: Attributes portName, serviceName and endpointInterface are not allowed in the @WebService annotation of an SEI.
	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.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
	at org.springframework.boot.SpringApplication.refreshContext(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)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.boot.devtools.restart.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);                
            }
            if (!StringUtils.isEmpty(sei)) {
                try {
                    seiClass = ClassLoaderUtils.loadClass(sei, implementorClass);
                } catch (ClassNotFoundException ex) {
                    throw new WebServiceException(BUNDLE.getString("SEI_LOAD_FAILURE_MSG"), ex);
                }
                WebService seiAnnotation = seiClass.getAnnotation(WebService.class);
                if (null == seiAnnotation) {
                    throw new WebServiceException(BUNDLE.getString("SEI_WITHOUT_WEBSERVICE_ANNOTATION_EXC"));
                }
                if (seiFromWsAnnotation 
                    && (!StringUtils.isEmpty(seiAnnotation.portName())
                    || !StringUtils.isEmpty(seiAnnotation.serviceName())
                    || !StringUtils.isEmpty(seiAnnotation.endpointInterface()))) {
                    String expString = BUNDLE.getString("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/*");
     }

你可能感兴趣的:(java)