2

I keep getting the following error when trying to deserialize an object containing a generic List<List<T>>:

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.lang.Object

I found that the exception is thrown when trying to deserialize the list, however if I change the field to be List<T>, it works. Please see the code below.

I have the following Java pojos (with getters and setters):

public class A<T> {
    private List<List<T>> values;
}

public class B extends A<Float>{
    @BsonId
    private Integer id;
}

My MongoDB client is configured with the following codec registry:

CodecRegistry codecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),
        fromProviders(PojoCodecProvider.builder()
            .conventions(Collections.singletonList(Conventions.ANNOTATION_CONVENTION))
            .automatic(true)
            .build()));

When I change the classes to the following structure, everything works:

public class A<T> {
    private List<T> values;
}


public class B extends A<List<Float>>{
    @BsonId
    private Integer id;
}

I excpect Mongo to support Generics at any level, but instead I get the following stacktrace:

Caused by: org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.lang.Object.
    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 org.bson.codecs.pojo.FallbackPropertyCodecProvider.get(FallbackPropertyCodecProvider.java:38)
    at org.bson.codecs.pojo.PropertyCodecRegistryImpl.get(PropertyCodecRegistryImpl.java:44)
    at org.bson.codecs.pojo.CollectionPropertyCodecProvider.get(CollectionPropertyCodecProvider.java:37)
    at org.bson.codecs.pojo.PropertyCodecRegistryImpl.get(PropertyCodecRegistryImpl.java:44)
    at org.bson.codecs.pojo.CollectionPropertyCodecProvider.get(CollectionPropertyCodecProvider.java:37)
    at org.bson.codecs.pojo.PropertyCodecRegistryImpl.get(PropertyCodecRegistryImpl.java:44)
    at org.bson.codecs.pojo.PojoCodecImpl.addToCache(PojoCodecImpl.java:211)
    at org.bson.codecs.pojo.PojoCodecImpl.specialize(PojoCodecImpl.java:80)
Eyal
  • 1,748
  • 2
  • 17
  • 31
Michal R
  • 21
  • 2
  • seems duplicate https://stackoverflow.com/questions/39433775/mongodb-java-inserting-throws-org-bson-codecs-configuration-codecconfigurationex – Qingfei Yuan Jun 17 '19 at 14:21
  • 1
    It's not the same problem. Here on deserialization, Mongo can't decide on the type (Float), and tries to code it as an Object. Also, I use the PojoCodecProvider as suggested. – Michal R Jun 17 '19 at 14:28
  • @MichalR It worked perfectly in my setup... Can you check [this](https://github.com/buraequete/sample-spring-mongo-embedded) ? What is different in your product? – buræquete Jul 22 '19 at 00:26

0 Answers0