title: Can't find a codec for my class (CodecConfigurationException)
body: <p>I need to save a java object in mongodb database. This object has an other object into it. When I try to save, I receive the error message on console:</p>
  
  <pre><code>org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class Item.
  </code></pre>
  
  <p>The problem is that I created the codec for class Item, but, it can't work even registering it. See the classes for best understanding.</p>
  
  <p>Models</p>
  
  <pre><code>public class Card(){
  
    public Card(){
     itens = new ArrayList&lt;Item&gt;()
    }
    String id;
    String description;
    List&lt;Item&gt; itens;
    .
    .
    .
  
  }
  </code></pre>
  
  <pre><code>public class Item(){
  
    String id;
    String name; 
    .
    .
    .
  
  }
  </code></pre>
  
  <p>CardCodec</p>
  
  <pre><code>public class CardCodec implements CollectibleCodec&lt;Card&gt;{
    private final Codec&lt;Document&gt; documentCodec;
  
    public CardCodec() {      
      this.documentCodec = new DocumentCodec();
    }
  
    @Override
    public void encode(BsonWriter writer, Card card, EncoderContext encoderContext) {
    Document cardDoc = new Document();
      String id = card.getId();
      String description = card.getDescription();  
      List&lt;Item&gt; itens = card.getItens();
  
      if(id != null) {
          cardDoc.put("_id", new ObjectId(id));
      }
      if(description != null){
          cardDoc.put("description", card.getDescription);
      }
      if(Itens != null){
          cardDoc.put("itens", card.getItens());
      }
      this.documentCodec.encode(writer, cardDoc, encoderContext);
    }
  
      @Override
      public Class&lt;Card&gt; getEncoderClass() {      
          return Card.class;
      }
  
      @Override
      public Card decode(BsonReader reader, DecoderContext decoderContext) {
          Document cardDoc = this.documentCodec.decode(reader, decoderContext);
          Card card=  new Card();
  
          card.setId(cardDoc .getObjectId("_id").toHexString());
          card.setDescription(cardDoc .getString("description"));
  
          List&lt;Document&gt; itensDoc =  cardDoc.getList("itens", Document.class);
          List&lt;Item&gt; itens = new ItemConverter().convertToListItem(itensDoc);
          card.setItens(itens);
          return card;
      }
  
      @Override
      public Card generateIdIfAbsentFromDocument(Card card) {
  
          if( !documentHasId(card) ) {
          card.setId(new ObjectId().toHexString());
          }
  
          return card;
  
      }
  
      @Override
      public boolean documentHasId(Card card) {       
          return null != card.getId();
      }
  
      @Override
      public BsonValue getDocumentId(Card card) {
          if(!documentHasId(card)) {
              throw new IllegalStateException("Esse documento não tem um _id");
          }
          return new BsonString(card.getId());
      }
  
  }
  </code></pre>
  
  <p>Converter</p>
  
  <pre><code>public class ItemConverter {
  
      public Item convert(Document doc) {
          Item item = new Item();
          String id = doc.getObjectId("_id").toHexString();
          String description = doc.getString("description");
          item.setId(id);
          item.setName(description);
          return item;
      }
  
      public List&lt;Item&gt; convertToListItem(List&lt;Document&gt; ItensDocs){      
          List&lt;Item&gt; itens = new ArrayList&lt;Item&gt;();   
          if(ItensDocs== null) {
             return Collections.emptyList();
          }
          for(Document doc: ItensDocs) {           
             itens.add( this.convert(doc) );
          }
          return itens;
      }
  
  }
  </code></pre>
  
  <p>ItemCodec</p>
  
  <pre><code>public class ItemCodec implements CollectibleCodec&lt;Item&gt;{
      private final Codec&lt;Document&gt; documentCodec;
  
      public ItemCodec() {    
          this.documentCodec = new DocumentCodec();
      }
  
      @Override
      public void encode(BsonWriter writer, Item value, EncoderContext encoderContext) {
          Document itemDoc = new Document();
  
          String id = value.getId();
          String name = value.getName();
  
          if(id != null) {
              itemDoc.put("_id", new ObjectId(id));
          }
          if(name != null) {
              itemDoc.put("name", value.getName());
          }
  
          documentCodec.encode(writer, itemDoc, encoderContext);
      }
  
      @Override
      public Class&lt;Item&gt; getEncoderClass() {      
          return Item.class;
      }
  
      @Override
      public Item decode(BsonReader reader, DecoderContext decoderContext) {      
          Document document = documentCodec.decode(reader, decoderContext);       
          return new ItemConverter().convert(document);
      }
  
      @Override
      public Item generateIdIfAbsentFromDocument(Item item) {
          if(!this.documentHasId(item)) {
              item.setId(new ObjectId().toHexString());           
          }
          return item;        
      }
  
      @Override
      public boolean documentHasId(Item item) {       
          return null != item.getId();
      }
  
      @Override
      public BsonValue getDocumentId(Item item) {
          if(!documentHasId(item)) {
              throw new IllegalStateException("Esse documento não tem um _id");
          }
  
          return new BsonString(item.getId());
  
      }
  
  
  }
  </code></pre>
  
  <p>Now, how I registry the codecs</p>
  
  <pre><code>...
      CardCodec cardCodec = new CardCodec();
      ItemCodec itemCodec = new ItemCodec();
  
      this.codecRegistry = CodecRegistries.fromRegistries(
                  MongoClientSettings.getDefaultCodecRegistry(),
                  CodecRegistries.fromCodecs(cardCodec, itemCodec)
      );
  
  this.cardCollection = this.mongoDatabase.getCollection("cards", Card.class).withCodecRegistry(this.codecRegistry);
  ...
  </code></pre>
  
  <p>When I try to execute an insert:</p>
  
  <pre><code>this.cardCollection.insertOne(card);
  </code></pre>
  
  <p>I receive: </p>
  
  <pre><code>org.bson.codecs.configuration.CodecConfigurationException
  </code></pre>