How to customize MappingMongoConverter (setMapKeyDotReplacement) in Spring-Boot without breaking the auto-configuration?

How could I customize the MappingMongoConverter within my Spring-Boot-Application (1.3.2.RELEASE) without changing any of the mongo-stuff which is autoconfigured by spring-data?

My current solution is:

@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 the right way or do I break some stuff with this? Or is there even a more simple way to set the mapKeyDotReplacement?


That's the right way to do it. The auto-configured MappingMongoConverter is annotated with @ConditionalOnMissingBean(MongoConverter.class), so adding your own MappingMongoConverter bean will cause the auto-configuration to back off in favour of your custom converter.

One minor correction: there's no need for you to call mongoConverter.afterPropertiesSet(). The container will call that for you.

(setMapKeyDotReplacement) in Spring-Boot without breaking the , How to customize MappingMongoConverter (setMapKeyDotReplacement) in Spring-Boot without breaking the auto-configuration? Ask Question. Asked 3 years  MongoDB-Escape dots '.' in map key] How to customize MappingMongoConverter (setMapKeyDotReplacement) in Spring-Boot without breaking the auto-configuration? 0.


I have run into this issue in the latest version of spring boot. Your approach did not work for me or the accepted answer...my boot app seemed to ignore my custom mapping converter.

So what I did in the config class I autowired in the MappingMongoConverter that boot uses and then set the setMapKeyDotReplacement on that.

@Autowired
private MappingMongoConverter mongoConverter;

// Converts . into a mongo friendly char
@PostConstruct
public void setUpMongoEscapeCharacterConversion() {
    mongoConverter.setMapKeyDotReplacement("_");
}

spring-boot, How to customize MappingMongoConverter (setMapKeyDotReplacement) in Spring-Boot without breaking the auto-configuration? - spring-boot. 5 How to customize MappingMongoConverter (setMapKeyDotReplacement) in Spring-Boot without breaking the auto-configuration? Jun 6 '18 4 Inheritance / instanceof checks in Freemarker Aug 26 '16


Also there's shorter version:

@Autowired
void setMapKeyDotReplacement(MappingMongoConverter mappingMongoConverter) {
    mappingMongoConverter.setMapKeyDotReplacement("_");
}

Remember to put it into class that Spring will be aware of - e.g. class annotated with @Configuration

User ZZ 5, How to set up MappingMongoConverter (setMapKeyDotReplacement) in Spring-​Boot without breaking auto-configuration? How can I configure  8 How to customize MappingMongoConverter (setMapKeyDotReplacement) in Spring-Boot without breaking the auto-configuration? Oct 3 '16 2 How can I write string to container to be used after a loop?


MappingMongoConverter setMapKeyDotReplacement dont work , How to customize MappingMongoConverter (setMapKeyDotReplacement) in Spring-Boot without breaking the auto-configuration? @Override  MongoDataAutoConfiguration now sets up packages for @document scanning. MongoDataAutoConfiguration now sets up the domain type scanning for packages defined via spring-data-mongodb.mappingBasePackages and falls back to all auto-configuration packages if nothing was configured explicitly.


User ZZ 5, 5 How to customize MappingMongoConverter (setMapKeyDotReplacement) in Spring-Boot without breaking the auto-configuration? View more network posts  Best way to add custom converters to spring-data-mongodb? - Demo.java


My code following this link: How to customize MappingMongoConverter (​setMapKeyDotReplacement) in Spring-Boot without breaking the auto-​configuration? Adds custom type information to the given Document if necessary. That is if the value is not the same as the one given. This is usually the case if you store a subtype of the actual declared type of the property.