Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mongo:mongo'. [11]

My xml only works up to version 1.8 of the spring-mongo.xsd schema. When I try with the latest (Currently 3.0) then I get these errors:

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] 

Here is my 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>

Based on other answers, I tried to use credentials attribute with a connection string but even that is getting rejected?

like image 346
ThreaT Avatar asked Nov 03 '21 14:11

ThreaT


1 Answers

You should fix the xsd version to your matching spring-data-mongo version. (to prevent surprises and not "rely on the latest") So:

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

To upgrade your xml to 3.0 (currently latest): See here


Table 4. Removed XML Namespace Elements and Attributes:

Element Attribute Replacement in 3.x Comment
<mongo:db-factory mongo-ref="…​" /> <mongo:db-factory mongo-client-ref="…​" /> Referencing a com.mongodb.client.MongoClient.
<mongo:mongo-client credentials="…​" /> <mongo:mongo-client credential="…​" /> 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] 
    

    Use mongo:mongo-client (It changed from 1.8 to 2.0).

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

    So, replace it with mongo-client-ref (referencing the client from 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] 
    

    You should use: credential attribute of mongo-client.

Please consider also Table 3 (writeConcern now with different values) as Table 5 (mongo-client-ref, connection-string).

For connection-string, best refer to (current)Mongo DB Manual.


Here is the edited xml after applying this answer, not sure if it's correct but I get no xml validation errors:

<?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>
like image 113
xerx593 Avatar answered Oct 19 '22 11:10

xerx593