【问题标题】:Spring Boot Standard UUID codec not working with AbstractMongoClientConfigurationSpring Boot 标准 UUID 编解码器不适用于 AbstractMongoClientConfiguration
【发布时间】:2020-02-23 04:28:59
【问题描述】:

我升级到 Spring Boot 2.2.0.RELEASE 并想用 AbstractMongoClientConfiguration 替换现在已弃用的 AbstractMongoConfiguration。我正在使用

codecRegistries.add(CodecRegistries.fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)));

将 MongoDB 中的 UUID Codec 设置为 STANDARD (UUID) 而不是 Legacy Codec (LUUID)。在查看数据库时,编解码器保持旧格式。其他人遇到过同样的问题吗?

旧实现(工作):

@Override
public MongoClient mongoClient() {
CodecRegistry codecRegistry =
                CodecRegistries.fromRegistries(CodecRegistries.fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)),
                        MongoClient.getDefaultCodecRegistry());
        return new MongoClient(new ServerAddress(address, port), MongoClientOptions.builder().codecRegistry(codecRegistry).build());
}

新的实现(不工作):

@Override
public MongoClient mongoClient() {
        List<CodecRegistry> codecRegistries = new ArrayList<>();
        codecRegistries.add(CodecRegistries.fromCodecs(new DocumentCodec()));
        codecRegistries.add(CodecRegistries.fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)));
        CodecRegistry codecRegistry = CodecRegistries.fromRegistries(codecRegistries);

        return MongoClients.create(MongoClientSettings.builder()
                                                      .codecRegistry(codecRegistry)
                                                      .applyConnectionString(new ConnectionString(connectionString))
                                                      .build());
}

我预计数据库中的 UUID 编解码器会调整为标准编解码器,但它仍保留在旧版编解码器中。

【问题讨论】:

    标签: spring mongodb spring-boot spring-data


    【解决方案1】:

    我写信是为了解决同样的问题。

    ?uuidRepresentation=STANDARD 附加到连接字符串。 它对我有用。

    【讨论】:

      【解决方案2】:

      我找到了解决问题的方法。 new UuidCodec(UuidRepresentation.STANDARD) 需要在第一个位置。我的代码如下所示:

          private static final CodecRegistry CODEC_REGISTRY = CodecRegistries.fromProviders(
              Arrays.asList(new UuidCodecProvider(UuidRepresentation.STANDARD),
                            new ValueCodecProvider(),
                            new BsonValueCodecProvider(),
                            new DBRefCodecProvider(),
                            new DBObjectCodecProvider(),
                            new DocumentCodecProvider(new DocumentToDBRefTransformer()),
                            new IterableCodecProvider(new DocumentToDBRefTransformer()),
                            new MapCodecProvider(new DocumentToDBRefTransformer()),
                            new GeoJsonCodecProvider(),
                            new GridFSFileCodecProvider(),
                            new Jsr310CodecProvider(),
                            new BsonCodecProvider()));
      

      这种行为不是很好,它可能是一个错误。希望这对你们中的一些人有所帮助。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      • 2020-05-26
      • 2015-04-13
      • 2018-10-18
      • 2017-06-09
      • 2019-12-28
      • 2017-02-27
      相关资源
      最近更新 更多