Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

471
Vistas
How to configure object mapping for MongoDB with Micronaut and Kotlin + KMongo?

I'm having some difficulty configuring object mapping for MongoDB in Micronaut with Kotlin. I'm getting errors like:

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.

With KMongo, this is easy. However the MongoClient injected by Micronaut doesn't have KMongo's codec registry.

I can get it working as follows:

    val db: MongoDatabase by lazy {
        val codecRegistry = ClassMappingType.codecRegistry(MongoClientSettings.getDefaultCodecRegistry())
        client.getDatabase("db-name").withCodecRegistry(codecRegistry)
    }

This code is taken directly from KMongo. (BTW, using database.withKMongo() resulted in the same error)

Although this works, what I'd like is to let Micronaut use KMongo to create the client, or let it use its codec like above, using configuration (application.yml).

There is a codec-registry setting mentioned here: https://micronaut-projects.github.io/micronaut-mongodb/latest/guide/configurationreference.html, but I have no idea what to enter in that setting to make it work.

Any help is appreciated!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can simply define the codec registry as a bean. Since you aren't in control of the class being registered you can create a factory

@Factory
class KMongoFactory {

    @Singleton
    fun kCodecRegistry(): CodecRegistry {
        return ClassMappingType.codecRegistry(MongoClientSettings.getDefaultCodecRegistry());
    }

}

Something like the above should do it

EDIT: Be aware that MongoClients.getDefaultCodecRegistry() is added by default

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda