【问题标题】: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.2.4.c:匹配通配符是严格的,但找不到元素“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 属性与连接字符串一起使用,但即使这样也被拒绝了?

【问题讨论】:

    标签: java spring mongodb


    【解决方案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.
    ... ... ...
    1. 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)。

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

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

    3. 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>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-11
    • 1970-01-01
    • 2017-08-13
    • 2014-11-18
    • 2018-03-21
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多