cvc-complex-type.2.4.c:匹配通配符是严格的,但找不到元素“mongo:mongo”的声明。 [11]

Posted

技术标签:

【中文标题】cvc-complex-type.2.4.c:匹配通配符是严格的,但找不到元素“mongo:mongo”的声明。 [11]【英文标题】:cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mongo:mongo'. [11] 【发布时间】:2021-12-17 22:21:05 【问题描述】:

我的 xml 仅适用于 spring-mongo.xsd 架构的 version 1.8。当我尝试使用latest(当前为3.0)时,我得到了这些错误:

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mongo:mongo'. [11] 
cvc-complex-type.3.2.2: Attribute 'mongo-ref' is not allowed to appear in element 'mongo:db-factory'. [12] 
cvc-complex-type.3.2.2: Attribute 'username' is not allowed to appear in element 'mongo:db-factory'. [12] 
cvc-complex-type.3.2.2: Attribute 'password' is not allowed to appear in element 'mongo:db-factory'. [12] 

这是我的 xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:mongo="http://www.springframework.org/schema/data/mongo"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/data/mongo
       http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">

    <mongo:mongo id="mongo" replica-set="$mongo.replicaset" write-concern="$mongo.write_concern:REPLICAS_SAFE"/>
    <mongo:db-factory id="DbFactory" mongo-ref="mongo" dbname="$mongo.db" username="$mongo.username" password="$mongo.password"/>
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg ref="DbFactory"/>
        <property name="writeResultChecking" value="EXCEPTION"/>
    </bean>
</beans>

根据其他答案,我尝试将credentials 属性与连接字符串一起使用,但即使这样也被拒绝了?

【问题讨论】:

【参考方案1】:

您应该将 xsd 版本修复为匹配的 spring-data-mongo 版本。 (为了防止意外,而不是“依赖最新的”) 所以:

xsi:schemaLocation="...
   http://www.springframework.org/schema/data/mongo
   http://www.springframework.org/schema/data/mongo/spring-mongo.YOUR.VERSION.xsd">

要将您的 xml 升级到 3.0(当前最新): See here


表 4. 删除的 XML 命名空间元素和属性:

Element Attribute Replacement in 3.x Comment
&lt;mongo:db-factory mongo-ref="…​" /&gt; &lt;mongo:db-factory mongo-client-ref="…​" /&gt; Referencing a com.mongodb.client.MongoClient.
&lt;mongo:mongo-client credentials="…​" /&gt; &lt;mongo:mongo-client credential="…​" /&gt; Single authentication data instead of list.
... ... ...
    cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mongo:mongo'. [11] 
    

    使用mongo:mongo-client(从1.8更改为2.0)。

    cvc-complex-type.3.2.2: Attribute 'mongo-ref' is not allowed to appear in element 'mongo:db-factory'. [12] 
    

    因此,将其替换为 mongo-client-ref(引用来自 1. 的客户端)。

    cvc-complex-type.3.2.2: Attribute 'username' is not allowed to appear in element 'mongo:db-factory'. [12] 
    cvc-complex-type.3.2.2: Attribute 'password' is not allowed to appear in element 'mongo:db-factory'. [12] 
    

    您应该使用:mongo-clientcredential 属性。

请同时考虑表 3(writeConcern 现在具有不同的值)作为表 5(mongo-client-refconnection-string)。

connection-string最好参考(current)Mongo DB Manual。


这是应用此答案后编辑的 xml,不确定它是否正确,但我没有收到 xml 验证错误:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:mongo="http://www.springframework.org/schema/data/mongo"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/data/mongo
       http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">

    <mongo:mongo-client id="mongo" replica-set="$mongo.replicaset" />
    <mongo:db-factory id="DbFactory" mongo-client-ref="mongo" dbname="$mongo.db" connection-string="mongodb://$mongo.username:$mongo.password@$mongo.db" write-concern="$mongo.write_concern:REPLICAS_SAFE" />
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg ref="DbFactory"/>
        <property name="writeResultChecking" value="EXCEPTION"/>
    </bean>
</beans>

【讨论】:

如果有人需要,这里是表格的链接。 github.com/spring-projects/spring-data-mongodb 关于REPLICAS_SAFE:javadoc:“更喜欢W2”!

以上是关于cvc-complex-type.2.4.c:匹配通配符是严格的,但找不到元素“mongo:mongo”的声明。 [11]的主要内容,如果未能解决你的问题,请参考以下文章

org.xml.sax.SAXParseException; cvc-complex-type.2.4.c:匹配的通配符是严格的,但找不到元素“mvc:resources”的声明

[bug]cxf: cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 ' 'jaxws:server' 的声明,问题处理

eclipse中不能找到dubbo.xsd报错”cvc-complex-type.2.4.c“的 两种解决方法

Multiple annotations found at this line: – cvc-complex-type.2.4.c: The matching wildcard is strict,

xml bug之cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration...

Type cvc-complex-type.2.4.c: The matching wildcard is strict...