SchemaCollection上的多个XSD:schemaElement不能为null吗?

时间:2019-01-11 11:56:45

标签: spring web-services soap xsd schema

我正在尝试在Web服务配置上使用SchemaCollection。但是,当我为此添加代码时,会收到此错误:

  

原因:java.lang.IllegalArgumentException:schemaElement不能为null!您是否运行afterPropertiesSet()或将其注册为Spring bean?

此错误是什么意思?在Spring code上看,与XSD上的schemaElement有关,但是我无法说什么。

淘汰schemaCollection,一切正常。

完整错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pedidos' defined in class path resource [br/com/company/rdt/config/WebServiceConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: schemaElement must not be null! Did you run afterPropertiesSet() or register this as a Spring bean?
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1631) ~[spring-beans-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481) ~[spring-beans-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) ~[spring-beans-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) ~[spring-beans-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.17.RELEASE.jar:1.5.17.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.17.RELEASE.jar:1.5.17.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.17.RELEASE.jar:1.5.17.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.17.RELEASE.jar:1.5.17.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.17.RELEASE.jar:1.5.17.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.17.RELEASE.jar:1.5.17.RELEASE]
    at br.com.company.rdt.IntegracaoRmsApplication.main(IntegracaoRmsApplication.java:12) [classes/:na]
Caused by: java.lang.IllegalArgumentException: schemaElement must not be null! Did you run afterPropertiesSet() or register this as a Spring bean?
    at org.springframework.util.Assert.notNull(Assert.java:134) ~[spring-core-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.xml.xsd.SimpleXsdSchema.getTargetNamespace(SimpleXsdSchema.java:95) ~[spring-xml-2.4.3.RELEASE.jar:na]
    at org.springframework.ws.wsdl.wsdl11.provider.InliningXsdSchemaTypesProvider.addTypes(InliningXsdSchemaTypesProvider.java:99) ~[spring-ws-core-2.4.3.RELEASE.jar:na]
    at org.springframework.ws.wsdl.wsdl11.ProviderBasedWsdl4jDefinition.afterPropertiesSet(ProviderBasedWsdl4jDefinition.java:234) ~[spring-ws-core-2.4.3.RELEASE.jar:na]
    at org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition.afterPropertiesSet(DefaultWsdl11Definition.java:183) ~[spring-ws-core-2.4.3.RELEASE.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1689) ~[spring-beans-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1627) ~[spring-beans-4.3.20.RELEASE.jar:4.3.20.RELEASE]
    ... 16 common frames omitted

bean声明:

@Bean(name = "orders")
    public DefaultWsdl11Definition defaultWsdl11Definition() {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("PedidosPort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://www.oracle.com/retail/igsla/integration/services/XYZFullPublishingService/v1");
        wsdl11Definition.setSchemaCollection(schemaCollection());
        wsdl11Definition.setRequestSuffix("Desc");
        wsdl11Definition.setResponseSuffix("Desc");
        return wsdl11Definition;
    }

    private XsdSchemaCollection schemaCollection() {
        return new XsdSchemaCollection() {

            public XsdSchema[] getXsdSchemas() {
                return new XsdSchema[]{new SimpleXsdSchema(new ClassPathResource("orders.xsd")), new SimpleXsdSchema(new ClassPathResource("po.xsd"))};
            }

            public XmlValidator createValidator() {
                throw new UnsupportedOperationException();
            }
        };
    }

XSD。 orders.xsd:

<s:schema xmlns:s="http://www.w3.org/2001/XMLSchema"
           xmlns:po="http://www.oracle.com/retail/integration/base/bo/XYZFullDesc/v1"
           targetNamespace="http://www.oracle.com/retail/abcd/integration/services/XYZFullPublishingService/v1"
          elementFormDefault="qualified">

    <s:import schemaLocation="po.xsd"
               namespace="http://www.oracle.com/retail/integration/base/bo/XYZFullDesc/v1" />

    <s:element name="publishXYZFullModifyUsingXYZFullDesc">
        <s:complexType>
            <s:sequence>
                <s:element ref="po:XYZFullDesc"/>
            </s:sequence>
        </s:complexType>
    </s:element>

</s:schema>

和po.xsd:

<?xml version="1.0" encoding="utf-16" ?>
<s:schema targetNamespace="http://www.oracle.com/retail/integration/base/bo/XYZFullDesc/v1"
           xmlns:s="http://www.w3.org/2001/XMLSchema"
           xmlns="http://www.oracle.com/retail/integration/base/bo/XYZFullDesc/v1"
           elementFormDefault="qualified">

    <s:element name="XYZFullDesc" type="XYZFullDesc"/>

    <s:complexType name="XYZFullDesc">
        <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="order_no" type="s:string"/>
        </s:sequence>
    </s:complexType>

</s:schema>

1 个答案:

答案 0 :(得分:0)

我不知道为什么,但是在将SchemaCollection更改为使用CommonsXsdSchemaCollection之后,错误消失了:

@Bean
public XsdSchemaCollection schemaCollection() {
    CommonsXsdSchemaCollection commonsXsdSchemaCollection = new CommonsXsdSchemaCollection(
            new ClassPathResource("xsd/po.xsd"),
            new ClassPathResource("xsd/orders.xsd"));
    commonsXsdSchemaCollection.setInline(true);
    return commonsXsdSchemaCollection;
}

此外,这里XSDs注册事项的顺序。如果在嵌套XSD(po.xsd)之前将父XSD(orders.xsd)声明为ClassPathResource,则会收到有关“名称空间重复”的错误。

我还需要添加对XML的依赖:

<dependency>
    <groupId>org.apache.ws.xmlschema</groupId>
    <artifactId>xmlschema-core</artifactId>
    <version>2.2.4</version>
</dependency>

为避免由于使用CommonsXsdSchemaCollection而导致的“找不到类”错误。