如何使用 Micronaut 和 Kotlin + KMongo 配置 MongoDB 的对象映射?
How to configure object mapping for MongoDB with Micronaut and Kotlin + KMongo?
我在使用 Kotlin 在 Micronaut 中为 MongoDB 配置对象映射时遇到了一些困难。我收到如下错误:
Decoding into a 'Asset' failed with the following exception:
Cannot find a public constructor for 'Asset'.
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
org.bson.codecs.configuration.CodecConfigurationException: An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'Asset' failed with the following exception:
Cannot find a public constructor for 'Asset'.
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
有了 KMongo,这很容易。然而,Micronaut 注入的 MongoClient 没有 KMongo 的编解码器注册表。
我可以让它工作如下:
val db: MongoDatabase by lazy {
val codecRegistry = ClassMappingType.codecRegistry(MongoClientSettings.getDefaultCodecRegistry())
client.getDatabase("db-name").withCodecRegistry(codecRegistry)
}
此代码直接取自 KMongo。 (顺便说一句,使用 database.withKMongo()
导致了同样的错误)
虽然这可行,但我想要的是让 Micronaut 使用 KMongo 创建客户端,或者让它像上面那样使用其编解码器,使用配置 (application.yml)。
此处提到了一个 codec-registry
设置:https://micronaut-projects.github.io/micronaut-mongodb/latest/guide/configurationreference.html,但我不知道在该设置中输入什么才能使其生效。
感谢任何帮助!
您可以简单地将编解码器注册表定义为一个 bean。由于您无法控制正在注册的 class,因此您可以创建一个工厂
@Factory
class KMongoFactory {
@Singleton
fun kCodecRegistry(): CodecRegistry {
return ClassMappingType.codecRegistry(MongoClientSettings.getDefaultCodecRegistry());
}
}
像上面这样的东西应该可以做到
编辑:请注意默认添加 MongoClients.getDefaultCodecRegistry()
我在使用 Kotlin 在 Micronaut 中为 MongoDB 配置对象映射时遇到了一些困难。我收到如下错误:
Decoding into a 'Asset' failed with the following exception:
Cannot find a public constructor for 'Asset'.
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type. org.bson.codecs.configuration.CodecConfigurationException: An exception occurred when decoding using the AutomaticPojoCodec. Decoding into a 'Asset' failed with the following exception:
Cannot find a public constructor for 'Asset'.
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
有了 KMongo,这很容易。然而,Micronaut 注入的 MongoClient 没有 KMongo 的编解码器注册表。
我可以让它工作如下:
val db: MongoDatabase by lazy {
val codecRegistry = ClassMappingType.codecRegistry(MongoClientSettings.getDefaultCodecRegistry())
client.getDatabase("db-name").withCodecRegistry(codecRegistry)
}
此代码直接取自 KMongo。 (顺便说一句,使用 database.withKMongo()
导致了同样的错误)
虽然这可行,但我想要的是让 Micronaut 使用 KMongo 创建客户端,或者让它像上面那样使用其编解码器,使用配置 (application.yml)。
此处提到了一个 codec-registry
设置:https://micronaut-projects.github.io/micronaut-mongodb/latest/guide/configurationreference.html,但我不知道在该设置中输入什么才能使其生效。
感谢任何帮助!
您可以简单地将编解码器注册表定义为一个 bean。由于您无法控制正在注册的 class,因此您可以创建一个工厂
@Factory
class KMongoFactory {
@Singleton
fun kCodecRegistry(): CodecRegistry {
return ClassMappingType.codecRegistry(MongoClientSettings.getDefaultCodecRegistry());
}
}
像上面这样的东西应该可以做到
编辑:请注意默认添加 MongoClients.getDefaultCodecRegistry()