Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

463
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda