title: Spring MongoDB - org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.lang.Class
body: <p>I am evaluating MongoDB and Spring Data MongoDB in a spring boot application for one of our upcoming project. I have a simple POJO which would be saved in MongoDB</p>
  
  <pre><code>@Document(collection = "customers")
  public class CustomerDoc{
  @org.springframework.data.annotation.Id
  private String Id;
  private String firstName;
  private String lastName;
  private MonetaryAmount amount;
  
      public CustomerDoc(String firstName, String lastName, MonetaryAmount    amount){
           this.firstName = firstName;
           this.lastName = lastName;
           this.amount = amount;
         }
  }
  </code></pre>
  
  <p>The Spring configuration is </p>
  
  <pre><code>@Bean
  public MongoDbFactory mongoDbFactory() throws Exception {
      MongoClient mongoClient = new MongoClient("localhost", 27017);
  
      return new SimpleMongoDbFactory(mongoClient, mongoDbName);
  }
  
  @Bean
  public MongoTemplate mongoTemplate() throws Exception {
      return new MongoTemplate(mongoDbFactory(), mongoConverter());
  }
  
  @Bean
  public MappingMongoConverter mongoConverter() throws Exception {
      MongoMappingContext mappingContext = new MongoMappingContext();
      DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory());
      MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mappingContext);
      mongoConverter.setMapKeyDotReplacement("\\:");
      return mongoConverter;
  }
  </code></pre>
  
  <p>In the configuration, I have to set the replacement key because without having it, I get exception:</p>
  
  <blockquote>
    <p>org.springframework.data.mapping.model.MappingException: Map key java.lang.Class contains dots but no replacement was configured! Make sure map keys don't contain dots in the first place or configure an appropriate replacement! </p>
  </blockquote>
  
  <p>This happens because of the <code>MonetaryAmount</code> object in the <code>Customer</code> class</p>
  
  <p>The customer repository is :</p>
  
  <pre><code>public interface CustomerDocRepository extends MongoRepository&lt;CustomerDoc, String&gt;{
  
      public List&lt;CustomerDoc&gt; findByFirstName();
  }
  </code></pre>
  
  <p>Now if I try to save a customer object, I get this exception:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.IllegalStateException: Failed to execute CommandLineRunner
        at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:675)
        at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:690)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
        at com.poc.mongodb.MongoDbApp.main(MongoDbApp.java:43)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
    Caused by: org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.lang.Class.
        at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
        at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
        at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
        at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:210)
        at com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:220)
        at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:196)
        at com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:220)
        at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:196)
        at com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:220)
        at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:196)
        at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:128)
        at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:61)
        at com.mongodb.CompoundDBObjectCodec.encode(CompoundDBObjectCodec.java:48)
        at com.mongodb.CompoundDBObjectCodec.encode(CompoundDBObjectCodec.java:27)
        at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
        at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
        at com.mongodb.connection.InsertCommandMessage.writeTheWrites(InsertCommandMessage.java:101)
        at com.mongodb.connection.InsertCommandMessage.writeTheWrites(InsertCommandMessage.java:43)
        at com.mongodb.connection.BaseWriteCommandMessage.encodeMessageBodyWithMetadata(BaseWriteCommandMessage.java:129)
        at com.mongodb.connection.RequestMessage.encodeWithMetadata(RequestMessage.java:160)
        at com.mongodb.connection.WriteCommandProtocol.sendMessage(WriteCommandProtocol.java:212)
        at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:101)
        at com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:67)
        at com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:37)
        at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
        at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286)
        at com.mongodb.connection.DefaultServerConnection.insertCommand(DefaultServerConnection.java:115)
        at com.mongodb.operation.InsertOperation.executeCommandProtocol(InsertOperation.java:76)
        at com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:141)
        at com.mongodb.operation.BaseWriteOperation$1.call(BaseWriteOperation.java:133)
        at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:230)
        at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:221)
        at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:133)
        at com.mongodb.operation.BaseWriteOperation.execute(BaseWriteOperation.java:60)
        at com.mongodb.Mongo.execute(Mongo.java:781)
        at com.mongodb.Mongo$2.execute(Mongo.java:764)
        at com.mongodb.DBCollection.executeWriteOperation(DBCollection.java:333)
        at com.mongodb.DBCollection.insert(DBCollection.java:328)
        at com.mongodb.DBCollection.insert(DBCollection.java:319)
        at com.mongodb.DBCollection.insert(DBCollection.java:289)
        at com.mongodb.DBCollection.insert(DBCollection.java:255)
        at com.mongodb.DBCollection.insert(DBCollection.java:239)
        at org.springframework.data.mongodb.core.MongoTemplate$10.doInCollection(MongoTemplate.java:1054)
        at org.springframework.data.mongodb.core.MongoTemplate$10.doInCollection(MongoTemplate.java:1049)
        at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:462)
        at org.springframework.data.mongodb.core.MongoTemplate.insertDBObjectList(MongoTemplate.java:1049)
        at org.springframework.data.mongodb.core.MongoTemplate.doInsertBatch(MongoTemplate.java:935)</p>
  </blockquote>
  
  <p>I am using mongo driver 3.2.2 Spring data MongoDB  1.9.1</p>
  
  <p>Any help would be really appreciated</p>