Spring Boot Standard UUID编解码器不适用于AbstractMongoClientConfiguration [英] Spring Boot Standard UUID codec not working with AbstractMongoClientConfiguration

查看:334
本文介绍了Spring Boot Standard UUID编解码器不适用于AbstractMongoClientConfiguration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I upgraded to Spring Boot 2.2.0.RELEASE and wanted to replace the now deprecated AbstractMongoConfiguration with AbstractMongoClientConfiguration. I am using

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

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

to set the UUID Codec in the MongoDB to STANDARD (UUID) instead of Legacy Codec (LUUID). When looking into the database the Codec stays in the legacy format. Anybody else experienced the same problem?

旧的实现方式(有效):

Old implementation (working):

@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());
}

新的实现方式(不起作用):

New implementation (not working):

@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编解码器可以调整为标准编解码器,但仍保留在旧版编解码器中.

I expected the UUID Codec in the database to adjust to Standard Codec but it stays in Legacy Codec.

推荐答案

我找到了解决该问题的方法. new UuidCodec(UuidRepresentation.STANDARD)必须位于第一个位置.我的代码如下:

I found a solution for the problem. The new UuidCodec(UuidRepresentation.STANDARD) needs to be at the first position. My Code looks like following:

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

这种行为不是很好,并且可能是一个错误.希望对您有所帮助.

That behavior is not very nice and it is possibly a bug. Hope this helps some of you.

这篇关于Spring Boot Standard UUID编解码器不适用于AbstractMongoClientConfiguration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆