How to set up MappingMongoConverter (setMapKeyDotReplacement) in Spring-Boot without breaking auto-configuration?

How can I configure MappingMongoConverter in my Spring-Boot-application (1.3.2.RELEASE) without changing any of the mongo-material that autoconfigure Spring-Data?

My current solution:

 @Configuration public class MongoConfig { @Autowired private MongoDbFactory mongoFactory; @Autowired private MongoMappingContext mongoMappingContext; @Bean public MappingMongoConverter mongoConverter() throws Exception { DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoFactory); MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mongoMappingContext); //this is my customization mongoConverter.setMapKeyDotReplacement("_"); mongoConverter.afterPropertiesSet(); return mongoConverter; } } 

Is this right or will I break something with this? Or is there an even easier way to set mapKeyDotReplacement?

+7
spring-boot spring-data spring-data-mongodb
source share
2 answers

This is the right way to do it. Auto-configured MappingMongoConverter annotated using @ConditionalOnMissingBean(MongoConverter.class) , so adding your own MappingMongoConverter bean will cancel the automatic configuration in favor of your custom converter.

One minor fix: you don't need to call mongoConverter.afterPropertiesSet() . The container will call this for you.

+5
source share

I ran into this problem in the latest spring boot version. Your approach did not work for me or the accepted answer ... my download application seemed to ignore my custom image converter.

So what did I do in the configuration class I, which was auto-programmed in MappingMongoConverter, which loads and then sets setMapKeyDotReplacement for it.

 @Autowired private MappingMongoConverter mongoConverter; // Converts . into a mongo friendly char @PostConstruct public void setUpMongoEscapeCharacterConversion() { mongoConverter.setMapKeyDotReplacement("_"); } 
+6
source share

All Articles