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)