Search code examples
jsonkotlinjavalinktorm

How to configure the default Jackson JSON Mapper on Javalin


So far, i found how to replace Javalin json mapper:

https://javalin.io/documentation#configuring-the-json-mapper

But i don't want to replace it, just want to add a few jackson modules, like this one:

https://www.ktorm.org/api-docs/org.ktorm.jackson/-ktorm-module/index.html

Without this, Javalin fails to serialize ktorm entities, sample code here


Solution

  • Solved!

    In JavalinConfig you can set an implementation of JsonMapper

    The default implementation accepts an ObjectMapper as parameter, so i can do this:

    // custom config to make ktor and jackson behave
    val mapper = ObjectMapper()
    mapper.registerModule(JavaTimeModule())
    mapper.registerModule(KotlinModule.Builder().build())
    mapper.registerModule(KtormModule())
    
    // spin up app
    val app = Javalin.create {
        it.jsonMapper(JavalinJackson(mapper))
    }.start(3000)
    

    And then Javalin and Ktorm works perfectly together.

    UPDATE:

    Now Javalin will detect KtormModule for you!