Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (5k points)

Our entire team is trying to set up our own Converters for Spring Data Mongo and having problems with it.

It feels that Spring never calls for registerConvertersIn on CustomConversions and therefore our custom converters are overridden. AbstractMongoConfiguration#customConversions never become part of conversion.

Currently, Spring Data Mongo 1.6.3 is being used, but it to be a problem for 1.8.0 too as I I've checked calls to CustomConversions#registerConvertersIn and found none.

I did fix this problem by calling CustomConversions#registerConvertersIn in custom MappingMongoConverter like this:

class MongoConfig extends AbstractMongoConfiguration {

    @Bean

    @Override

    public MappingMongoConverter mappingMongoConverter() throws Exception {

        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory());

        MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext()) {

            @Override

            public void setCustomConversions(CustomConversions conversions) {

                super.setCustomConversions(conversions);

                conversions.registerConvertersIn(conversionService);

            }

        };

        converter.setCustomConversions(customConversions());

        return converter;

    }

}

 

Are we committing any mistake over here?

1 Answer

0 votes
by (10.2k points)

You need to manage class by using the Spring BeanFactory to get callback afterPropertiesSet() ( where conversions.registerConvertersIn(conversionService) is originally called ) automatically called. 

In case you don't annotate the configuration bean then you have to call afterPropertiesSet() by yourself. 


If you want to learn MongoDB, checkout this MongoDB tutorial from Intellipaat.

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...