Spring data MongoDb: MappingMongoConverter remove _class

后端 未结 10 919
迷失自我
迷失自我 2020-12-04 21:48

The default MappingMongoConverter adds a custom type key (\"_class\") to each object in the database. So, if I create a Person:

package my.d         


        
相关标签:
10条回答
  • 2020-12-04 22:27
    <mongo:mongo host="hostname" port="27017">
    <mongo:options
    ...options...
    </mongo:mongo>
    <mongo:db-factory dbname="databasename" username="user" password="pass"                     mongo-ref="mongo"/>
    <bean id="mongoTypeMapper"     class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
    <constructor-arg name="typeKey"><null/></constructor-arg>
    </bean>
    <bean id="mongoMappingContext"      class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
    <bean id="mongoConverter"     class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    <constructor-arg name="mappingContext" ref="mongoMappingContext" />
    <property name="typeMapper" ref="mongoTypeMapper"></property>
    </bean>
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
    <constructor-arg name="mongoConverter" ref="mongoConverter" />
    <property name="writeResultChecking" value="EXCEPTION" /> 
    </bean>
    
    0 讨论(0)
  • 2020-12-04 22:27

    While, Mkyong's answer still works, I would like to add my version of solution as few bits are deprecated and may be in the verge of cleanup.

    For example : MappingMongoConverter(mongoDbFactory(), new MongoMappingContext()) is deprecated in favor of new MappingMongoConverter(dbRefResolver, new MongoMappingContext()); and SimpleMongoDbFactory(new Mongo(), "databasename"); in favor of new SimpleMongoDbFactory(new MongoClient(), database);.

    So, my final working answer without deprecation warnings is :

    @Configuration
    public class SpringMongoConfig {
    
        @Value("${spring.data.mongodb.database}")
        private String database;
    
        @Autowired
        private MongoDbFactory mongoDbFactory;
    
        public @Bean MongoDbFactory mongoDBFactory() throws Exception {
            return new SimpleMongoDbFactory(new MongoClient(), database);
        }
    
        public @Bean MongoTemplate mongoTemplate() throws Exception {
    
            DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
    
            // Remove _class
            MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, new MongoMappingContext());
            converter.setTypeMapper(new DefaultMongoTypeMapper(null));
    
            return new MongoTemplate(mongoDBFactory(), converter);
    
        }
    
    }
    

    Hope this helps people who would like to have a clean class with no deprecation warnings.

    0 讨论(0)
  • 2020-12-04 22:28

    you just need to add the @TypeAlias annotation to the class defintion over changing the type mapper

    0 讨论(0)
  • 2020-12-04 22:29

    This is my one line solution:

    @Bean 
    public MongoTemplate mongoTemplateFraud() throws UnknownHostException {
    
      MongoTemplate mongoTemplate = new MongoTemplate(getMongoClient(), dbName);
      ((MappingMongoConverter)mongoTemplate.getConverter()).setTypeMapper(new DefaultMongoTypeMapper(null));//removes _class
      return mongoTemplate;
    }
    
    0 讨论(0)
提交回复
热议问题