uuidconfigurationexception in kmogo Kotlin

233 Views Asked by At
import org.litote.kmongo.KMongo
fun main() {
    val client = Kmongo.createClient(/* connection string from mongodb */)
    val database = client.getDatabase(/* databaseName */)

}

my code ^

this is what it returns:

Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Changing the default UuidRepresentation requires a CodecRegistry that also implements the CodecProvider interface
    at org.litote.kmongo.KMongo.createRegistry(KMongo.kt:89)
    at org.litote.kmongo.KMongo.createClient(KMongo.kt:78)
    at org.litote.kmongo.KMongo.createClient(KMongo.kt:60)
    at org.litote.kmongo.KMongo.createClient(KMongo.kt:50)
    at MainKt.main(Main.kt:3) 
    at MainKt.main(Main.kt)

for security purposes, i have omitted the additional code that are irrelevant and the database names

Thanks for your help in advance!

1

There are 1 best solutions below

0
On

From kmongo source here: https://github.com/Litote/kmongo/blob/master/kmongo-core/src/main/kotlin/org/litote/kmongo/KMongo.kt, it seems that this is happening because the UUID Representation is JAVA_LEGACY.

Hence, you should supply a MongoClientSettings object to createClient, which specifies a UUID representation other than JAVA_LEGACY. Since UuidRepresentation is an enum (https://mongodb.github.io/mongo-java-driver/3.5/javadoc/org/bson/UuidRepresentation.html), you can try using STANDARD.

Like this:

val settings = MongoClientSettings.builder()
.applyConnectionString(ConnectionString("Connection String here"))
.uuidRepresentation(UuidRepresentation.STANDARD)
.codecRegistry(KMongoUtil.defaultCodecRegistry)
.build()
val client = KMongo.createClient(settings)
// other code below...

This should get KMongo/MongoDB to use UuidRepresentation.STANDARD instead of UuidRepresentation.JAVA_LEGACY (in which KMongo will always throw an exception)

Thanks!