When an enum exists in the same package as POJO classes, its type is handled as a POJO and not as an enum.
Consider the package "my_package" and under it the following:
package my_package; public class MyClass { public enum MyEnum { ENUM_1, ENUM_2; } private MyEnum e; public MyClass() { } public MyEnum getE() {return e;} public void setE(MyEnum e) {this.e = e;} } // In another package, where MongoClient is created //Register the PojoCodecProvider PojoCodecProvider pojoCodecProvider = PojoCodecProvider.builder() .register("my_package").build(); //.... Rest of the code that associates PojoCodecProvider with MongoClient //When find() is called to bring a MyClass document from the database: MyClass myClass = myClassCollection.find().first();
The following error occurs: Caused by: org.bson.codecs.configuration.CodecConfigurationException: Cannot find a public constructor for 'MyEnum'. at org.bson.codecs.pojo.CreatorExecutable.checkHasAnExecutable(CreatorExecutable.java:140) at org.bson.codecs.pojo.CreatorExecutable.getInstance(CreatorExecutable.java:107) at org.bson.codecs.pojo.InstanceCreatorImpl.<init>(InstanceCreatorImpl.java:40) at org.bson.codecs.pojo.InstanceCreatorFactoryImpl.create(InstanceCreatorFactoryImpl.java:28) at org.bson.codecs.pojo.ClassModel.getInstanceCreator(ClassModel.java:70) at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:121) at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:126) at org.bson.codecs.pojo.LazyPojoCodec.decode(LazyPojoCodec.java:57) at org.bson.codecs.DecoderContext.decodeWithChildContext(DecoderContext.java:93) at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:184) ... 67 more
The workaround on this issue might be registering specific classes in PojoCodecProvider rather than a package name, but its not very convenient.
Additionally, creating and registering a custom codec for "MyEnum" enum doesn't help since it seems to be overridden by a PojoCodec.
Thanks,
Igal