I'm trying to retrieve documents from Mongo DB. The documents received well when the Gender attribute is not included. But when it's included an error occurs.
Here is my function to retrieve the person documents from Mongo
private List<Person> getPersons()
{
// Query to get person list
// Loop through person list
for (Person person : personList) {
// Some condition checking
filteredPersonList.add(person);
}
return filteredPersonList;
}
Here is my Person class
public class Person {
@BsonProperty("firstName")
@JSONField("firstName")
public String FirstName
@BsonProperty("lastName")
@JSONField("lastName")
public String LastName
@BsonProperty("gender")
@JSONField("gender")
public Gender Gender
@BsonProperty("isMarried")
@JSONField("isMarried")
public Boolean IsMarried
}
Here is the Gender enum for the Gender property
@JSONType(serializeEnumAsJavaBean = true)
public enum Gender {
Male(0),
Female(1),
;
private final int type;
Gender (int type) {
this.type = type;
}
public int getType() {
return type;
}
}
This is the error I get
org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'Person'. Decoding 'gender' errored with: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is INT32.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:224)
at org.bson.codecs.pojo.PojoCodecImpl.decodeProperties(PojoCodecImpl.java:197)
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:121)
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:125)
at org.bson.codecs.pojo.LazyPojoCodec.decode(LazyPojoCodec.java:57)
at org.bson.codecs.DecoderContext.decodeWithChildContext(DecoderContext.java:96)
at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:218)
... 58 more
In Mongo DB, the type of the gender property is INT32.