title: mongoDb and spring data custom converter for enum field
body: <p>I have a simple Java class called Metric which has two fields: MetricType and value:</p>
  
  <pre><code>public class Metric {
      MetricType type;
      int value;
  }
  
  enum MetricType {
      SPACE, CPU, UNKNOWN
  }
  </code></pre>
  
  <p>When reading the metrics from mongo I want the a custom convertor for MetricType which will convert anything which is not mapped to enum to UNKNOWN.</p>
  
  <p>My repository is a simple repository : </p>
  
  <pre><code>public interface MetricRepository extends MongoRepository&lt;Metric, 
  String&gt; {}
  </code></pre>
  
  <p>I am using spring-boot-starter-data-mongodb version 1.5.9</p>
  
  <p>what I tried doing is creating a Converter from string to MetricType</p>
  
  <pre><code>@ReadingConverter
  public class StringToMetricTypeConverter implements Converter&lt;String, MetricType&gt; {
  
      @Override
      public MetricType convert(String dbData) {
           try {
               return MetricType.valueOf(dbData);
           }
          catch (IllegalArgumentException e) {
              return MetricType.UNKNOWN;
          }
       }
   }
  </code></pre>
  
  <p>and in the mongoConfig file adding:</p>
  
  <pre><code>@Bean
  public MongoTemplate mongoTemplate() throws Exception {
      MongoTemplate mongoTemplate = new MongoTemplate(mongo(), getDatabaseName());
      MappingMongoConverter mongoMapping = (MappingMongoConverter) mongoTemplate.getConverter();
      mongoMapping.setCustomConversions(customConversions()); 
      mongoMapping.afterPropertiesSet();
      return mongoTemplate;
  }
  
  @Bean
  public CustomConversions customConversions() {
      return new CustomConversions(Arrays.asList(new StringToMetricTypeConverter()));
  }
  </code></pre>
  
  <p>I do see the converter is registered in the mongoTemplate but the converter is not being called... what am I missing here?</p>
  
  <p>thanks!</p>