我有一个问题。我使用2.x版本,请查看源代码。MappingMongoConverter.class:
@Nullable
private <S> S read(TypeInformation<S> type, @Nullable Bson bson, ObjectPath path) {
if (null == bson) {
return null;
} else {
TypeInformation<? extends S> typeToUse = this.typeMapper.readType(bson, type);
Class<? extends S> rawType = typeToUse.getType();
if (this.conversions.hasCustomReadTarget(bson.getClass(), rawType)) {
return this.conversionService.convert(bson, rawType);
} else if (DBObject.class.isAssignableFrom(rawType)) {
return bson;
} else if (Document.class.isAssignableFrom(rawType)) {
return bson;
} else if (typeToUse.isCollectionLike() && bson instanceof List) {
return this.readCollectionOrArray(typeToUse, (List)bson, path);
} else if (typeToUse.isMap()) {
return this.readMap(typeToUse, bson, path);
} else if (bson instanceof Collection) {
throw new MappingException(String.format("Cannot convert %1$s of type %2$s into an instance of %3$s! Implement a custom Converter<%2$s, %3$s> and register it with the CustomConversions. Parent object was: %4$s", bson, BasicDBList.class, typeToUse.getType(), path));
} else if (typeToUse.equals(ClassTypeInformation.OBJECT)) {
return bson;
} else {
Document target = bson instanceof BasicDBObject ? new Document((BasicDBObject)bson) : (Document)bson;
MongoPersistentEntity<?> entity = (MongoPersistentEntity)this.mappingContext.getPersistentEntity(typeToUse);
if (entity == null) {
throw new MappingException(String.format("Expected to read Document %s into type %s but didn't find a PersistentEntity for the latter!", target, typeToUse.getType()));
} else {
return this.read((MongoPersistentEntity)this.mappingContext.getRequiredPersistentEntity(typeToUse), target, path);
}
}
}
}
此行:
else if (DBObject.class.isAssignableFrom(rawType)) {
return bson;
}
当我尝试将响应转换为DBObject时,这将返回bson.class(Document.class),例如:
List<DBObject> userList = mongoTemplate.findAll(DBObject.class, "user_info");
“ userList”元素实际上是Document.class。因此,在执行此代码时:
DBObject obj = userList.get(1)
这是一个问题:“ org.bson.Document无法转换为com.mongodb.DBObject。”
对吗?为什么我执行:mongoTemplate.findAll(DBObject.class,“ user_info”),我将使用List的Document.class?不是列表的DBObject.class?