Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (6.1k points)
closed by

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?

closed

4 Answers

0 votes
by (7.8k points)
 
Best answer
To set up custom converters in Spring Data Mongo, override the mappingMongoConverter() method in your MongoConfig class. Create a custom MappingMongoConverter and override the setCustomConversions() method. Inside this method, call conversions.registerConvertersIn(conversionService) to register your custom converters. Set the custom conversions by calling converter.setCustomConversions(customConversions()). This approach ensures proper registration of your custom converters.
0 votes
by (11.7k 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.

0 votes
by (11.4k points)
The issue you are facing with setting up custom converters in Spring Data Mongo can be resolved by calling CustomConversions#registerConvertersIn in the custom MappingMongoConverter class.

In your MongoConfig class, override the mappingMongoConverter() method and create a new instance of MappingMongoConverter.

Inside the custom MappingMongoConverter, override the setCustomConversions() method and call conversions.registerConvertersIn(conversionService) to register your custom converters.

Make sure to set the custom conversions by calling converter.setCustomConversions(customConversions()).

This approach ensures that your custom converters are properly registered and not overridden, resolving the problem you are encountering.
0 votes
by (13k points)

Override mappingMongoConverter() in MongoConfig class, create a custom MappingMongoConverter, and call conversions.registerConvertersIn(conversionService) inside setCustomConversions().

Related questions

0 votes
1 answer
0 votes
1 answer
asked Jul 30, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...