Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

473
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!