首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >kmogo Kotlin中的uuidconfigurationexception

kmogo Kotlin中的uuidconfigurationexception
EN

Stack Overflow用户
提问于 2022-04-27 03:21:51
回答 1查看 138关注 0票数 0
代码语言:javascript
复制
import org.litote.kmongo.KMongo
fun main() {
    val client = Kmongo.createClient(/* connection string from mongodb */)
    val database = client.getDatabase(/* databaseName */)

}

我的代码^

这就是它返回的内容:

代码语言:javascript
复制
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)

出于安全考虑,我省略了不相关的附加代码和数据库名称。

谢谢你提前帮忙!

EN

回答 1

Stack Overflow用户

发布于 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

如下所示:

代码语言:javascript
复制
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总是抛出异常)。

谢谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72022889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档