How to Set the URI for the ClusterSettings with codec in mongodb java driver

327 Views Asked by At

I am using the codec for the serialization and deserialization of POJO using 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);
    }
}

Trying to set applyToClusterSettings, but quite not sure how can I set the MongoDB URI

1

There are 1 best solutions below

1
On
.applyConnectionString(new ConnectionString(mongodbConfiguration.uri()))