import org.litote.kmongo.KMongo
fun main() {
val client = Kmongo.createClient(/* connection string from mongodb */)
val database = client.getDatabase(/* databaseName */)
}我的代码^
这就是它返回的内容:
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)出于安全考虑,我省略了不相关的附加代码和数据库名称。
谢谢你提前帮忙!
发布于 2022-04-29 06:12:03
从这里的kmongo源代码:https://github.com/Litote/kmongo/blob/master/kmongo-core/src/main/kotlin/org/litote/kmongo/KMongo.kt,这似乎是因为UUID表示形式是JAVA_LEGACY。
因此,您应该向MongoClientSettings提供一个createClient对象,该对象指定JAVA_LEGACY以外的UUID表示形式。因为UuidRepresentation是枚举(https://mongodb.github.io/mongo-java-driver/3.5/javadoc/org/bson/UuidRepresentation.html),所以可以尝试使用STANDARD。
如下所示:
val settings = MongoClientSettings.builder()
.applyConnectionString(ConnectionString("Connection String here"))
.uuidRepresentation(UuidRepresentation.STANDARD)
.codecRegistry(KMongoUtil.defaultCodecRegistry)
.build()
val client = KMongo.createClient(settings)
// other code below...这将使KMongo/MongoDB使用UuidRepresentation.STANDARD而不是UuidRepresentation.JAVA_LEGACY (在这种情况下,KMongo总是抛出异常)。
谢谢!
https://stackoverflow.com/questions/72022889
复制相似问题