我升级到Spring Boot 2.2.0.RELEASE,并想用AbstractMongoClientConfiguration代替现在不推荐使用的AbstractMongoConfiguration。我正在使用
codecRegistries.add(CodecRegistries.fromCodecs(new UuidCodec(UuidRepresentation.STANDARD)));
将MongoDB中的UUID编解码器设置为STANDARD(UUID),而不是传统编解码器(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编解码器可以调整为标准编解码器,但仍保留在旧版编解码器中。
答案 0 :(得分:1)
我找到了解决问题的办法。 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()));
这种行为不是很好,并且可能是一个错误。希望对您有所帮助。
答案 1 :(得分:0)
我写信来解决这个问题。
将?uuidRepresentation=STANDARD
附加到connectionString。
它对我有用。