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

Multiple XSDs on SchemaCollection: schemaElement must not be null?

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

Caused by: java.lang.IllegalArgumentException: schemaElement must not be null! Did you run afterPropertiesSet() or register this as a 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.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>

我不知道为什么,但是将 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,您将收到一些关于 "namespace duplicated" 的错误.

我还需要在 XML:

上添加这种依赖
<dependency>
    <groupId>org.apache.ws.xmlschema</groupId>
    <artifactId>xmlschema-core</artifactId>
    <version>2.2.4</version>
</dependency>

防止因使用 CommonsXsdSchemaCollection.

而导致的 "class not found" 错误