redben
redben

Reputation: 5686

Camel MongoDB : how to add cutsom converters

I have looked at the documentation but couldn't find how to add custom type converters for mongoDB. How can one do it ?

Upvotes: 0

Views: 671

Answers (1)

udalmik
udalmik

Reputation: 7998

First of all I think that fromAnyObjectToDBObject default converter can resolve most of your cases. It uses Jackson library and all you need is to mark your custom classes with right annotations.

Anyway, if you still need custom converter, following steps should be performed:

  1. White your conversion logic and mark your class and converter methods with org.apache.camel.Converter annotation:

    package com.acme.converters;
    
    @Converter
    public class CustomConverter {
    
      @Converter
      public static DBObject fromCustomToDBObject(CustomType object) {
        // conversion logic
      }
    
    }
    
  2. Create following resource file to be placed in your result jar:

META-INF/services/org/apache/camel/TypeConverter

And list your converter classes in this file:

com.acme.converters.CustomConverter

This file will help Camel to auto-discover your own converters.

You can also take a look at:

  • META-INF/services/org/apache/camel/TypeConverter in camel-mongodb-x.x.x.jar to see how default converter defined
  • org.apache.camel.impl.converter.AnnotationTypeConverterLoader class to check converters loading logic

Upvotes: 2

Related Questions