我正在尝试从Mongo DB检索文档。当不包括性别属性时,文件得到了很好的接收。但是当它被包含进来时,就会发生错误。
下面是我从Mongo检索person文档的函数
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;
}
这是我的Person类
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
}
下面是Gender属性的性别枚举
@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;
}
}
这是我得到的错误
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
在Mongo DB中,性别属性的类型是INT32。
发布于 2021-10-01 07:45:33
https://stackoverflow.com/questions/68802903
复制相似问题