由于这是在谷歌中搜索错误时的第一个结果,并且接受的答案中的解决方案似乎并不那么简单,并且似乎不再有效,因为 MongoClient 不再有 getDefaultCodecRegistry() ,我将发布对我有帮助的内容:
protected MongoCollection<Document> collection;
private final CodecRegistry DEFAULT_REGISTRY = CodecRegistries.fromProviders(
asList(new ValueCodecProvider(),
new BsonValueCodecProvider(),
new DocumentCodecProvider(),
new DBRefCodecProvider(),
new DBObjectCodecProvider(),
new BsonValueCodecProvider(),
new GeoJsonCodecProvider(),
new GridFSFileCodecProvider()));
private final BsonTypeClassMap DEFAULT_BSON_TYPE_CLASS_MAP = new BsonTypeClassMap();
private final DocumentCodec documentCodec = new DocumentCodec(
DEFAULT_REGISTRY,
DEFAULT_BSON_TYPE_CLASS_MAP
);
-----------------------------------------------------------------------
JsonWriterSettings writerSettings = org.bson.json.JsonWriterSettings.
builder().
outputMode(JsonMode.SHELL).
indent(true)
.build();
JsonArray jsonArray = new JsonArray();
collection.
find().
iterator().
forEachRemaining(entry -> jsonArray.add(new JsonParser().parse(entry.toJson(writerSettings, documentCodec)).getAsJsonObject()));
解决方案来自这里:https ://github.com/akitoshka/debezium/commit/8dd12d76acced74de7ab184bc18a4384565a70b7