【发布时间】:2021-10-21 16:22:12
【问题描述】:
我正在使用编解码器进行 POJO 的序列化和反序列化,使用 4.3/driver-reactive http://mongodb.github.io/mongo-java-driver/4.3/driver-reactive/getting-started/quick-start-pojo/
@Singleton
public record Repository(MongodbConfiguration mongodbConfiguration) implements IRepository {
@Override
public <T> MongoCollection<T> getCollection(String collectionName, Class<T> typeParameterClass) {
// create codec registry for POJOs
CodecRegistry pojoCodecRegistry = fromProviders(PojoCodecProvider.builder().automatic(true).build());
CodecRegistry codecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), pojoCodecRegistry);
MongoClientSettings clientSettings = MongoClientSettings.builder()
.applyToClusterSettings() // need to set the URI
.codecRegistry(codecRegistry)
.addCommandListener(new MongoCommand())
.build();
MongoClient mongoClient = MongoClients.create(clientSettings);
return mongoClient
.getDatabase(mongodbConfiguration.database())
.getCollection(collectionName, typeParameterClass);
}
}
尝试设置applyToClusterSettings,但不太确定如何设置 MongoDB URI
【问题讨论】:
标签: java micronaut mongodb-java mongodb-java-3.3.0 mongodb-java-3.8