Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring not using mongo custom converters

I have been trying to register my own write custom converters to change the default ID value. But it never actually calls. Here is my Custom Converter

public class EventKeyConverter implements Converter<Event,DBObject> {

    @Override
    public DBObject convert(Event object) {
        DBObject dbObject = DBObjectTransformer.toDBObject(object);
        dbObject.put("_id", KeyGenerator.getRandomKey());
        return dbObject;
    }

}

here is the place that I did register customer converter

@Override
@Bean
public CustomConversions customConversions() {
    List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>();
    converters.add(new EventKeyConverter());
    return new CustomConversions(converters);
}

@Override
@Bean
public MappingMongoConverter mappingMongoConverter() throws Exception {
    MappingMongoConverter converter = new MappingMongoConverter(
            eventsMongoDbFactory(), mongoMappingContext());
    converter.setCustomConversions(customConversions());
    return converter;
}

@Bean
public MongoTemplate eventsMongoTemplate() throws Exception {
    final MongoTemplate template = new MongoTemplate(
            eventsMongoDbFactory(), mappingMongoConverter());
    template.setWriteResultChecking(WriteResultChecking.EXCEPTION);

    return template;
}

When I'm saving some objects this converter never gets called.


Edit 1 : I need to change the default object Id into some custom Id (UUID + random key) in all repositories. That why I tried to use mongo converter.

Edit 2 : Just found the issue. Change @Configuration to @Component in class that contains customConversion() and it works fine. But still wondering what is happened here ?

like image 216
Rajith Delantha Avatar asked Feb 18 '13 06:02

Rajith Delantha


People also ask

Which is better MongoTemplate or MongoRepository?

So I'd say that MongoTemplate is a better option, unless you have a very elaborated POJO model or need the custom queries capabilities of MongoRepository for some reason. Good points/examples. However your race condition example and undesired result can be avoided using @Version to prevent that very scenario.

Can I use Spring data JPA with MongoDB?

Yes, DataNucleus JPA allows it, as well as to many other databases.

Does Spring support MongoDB?

The Spring framework provides powerful connectors to easily perform database operations with MongoDB. Data is stored as BSON objects in MongoDB making data retrieval easy. For this Spring Boot and MongoDB example tutorial, we are only concerned with the Persistence and Database layers.

Does spring boot provide MongoDB auto configuration?

Spring Boot provides auto-configuration for Redis, MongoDB, Elasticsearch, Solr and Cassandra; you can make use of the other projects, but you will need to configure them yourself.


1 Answers

@Configuration defines a Spring context fragment that contains methods that if annotated with @Bean return new beans and put them in the context.

@Component is a way of saying "this Pojo is a Spring bean". You then need a @ComponentScan annotation or XML equivalent to scan packages for @Component annotated beans.

So in your case, you created the converter fine, but it wasn't registered as a Spring bean until you added the @Component, so it didn't work initially.

like image 163
PaulNUK Avatar answered Nov 04 '22 05:11

PaulNUK