Resolve DBRef into Json

后端 未结 2 1801
清酒与你
清酒与你 2021-01-18 12:36

I\'m getting the following error in a Normalized Data Model structure in MongoDB:

org.bson.codecs.configuration.CodecConfigurationException: Can\'t find a co         


        
2条回答
  •  孤街浪徒
    2021-01-18 13:21

    Since this is the first result when searching for the error in google and the solution in the accepted answer doesn't seem that straightforward and no longer seems to work since MongoClient does not have a getDefaultCodecRegistry() any longer, I will post what helped me:

        protected MongoCollection 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()));
    

    The solution comes from here: https://github.com/akitoshka/debezium/commit/8dd12d76acced74de7ab184bc18a4384565a70b7

提交回复
热议问题