I have a POJO that has a list of Resource
s which is the interface of ResourceType1
and ResourceType2
public class MyPojo {
private List<Resource>;
....
}
public interface Resource {
public String getResourceId();
}
public ResourceType1 implements Resource{
public String getResourceId(){ return resourceType1Id; }
public String doOtherResourceType1SpecificStuff(){}
}
public ResourceType2 implements Resource{
public String getResourceId(){ return resourceType2Id; }
public boolean doOtherResourceType2SpecificStuff(){}
}
public class Database {
CodecRegistry pojoCodecRegistry = fromRegistries(
MongoClient.getDefaultCodecRegistry(),
fromProviders(PojoCodecProvider.builder()
.register(MyPojo.class)
.register(ResourceType1.class)
.register(ResourceType2.class)
.register(Resource.class)
.automatic(true).build()));
}
Im using mongodb-driver-core-3.5.0
in my Database class to save and retrieve myPojo
's.
Im get this on error on save and the docuement is created, but the list of resources is empty when I use MongoCompass to inspect the saved document.
This is the error:
org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'resources'. An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'Resource' failed with the following exception:
Cannot find a public constructor for 'Resource'.
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:173)
at org.bson.codecs.pojo.PojoCodecImpl.decodeProperties(PojoCodecImpl.java:149)
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:103)
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:107)
...
I've had a similar problem in the past, and just wrote a MyPojoWrapper
where I marshalled and un-marshalled everything manually. However I have to believe this is more common and there is an easy fix I don't see. Thank you in advance.