kotlinhttp4k

How to configure Jackson mapper


How can I globally configure json serializer for http4k? For example, snake case field names or formatting DateTime as ISO8601.


Solution

  • Since the ObjectMapper instance is private within ConfigurableJackson you cannot get at it after construction to do any configuration.

    So you either need to construct your own direct instance of ConfigurableJackson and pass in a customized ObjectMapper or you need to subclass ConfigurableJackson with your own class. And then during the constructor, create an ObjectMapper (see example below) or intercept one being passed into your constructor and change its settings.

    Whatever you do, be sure you do not break the http4k framework or anything else that might be using the same instance. You can see the defaults used by http4k declared in their source code:

    object Jackson : ConfigurableJackson(ObjectMapper()
        .registerModule(defaultKotlinModuleWithHttp4kSerialisers)
        .disableDefaultTyping()
        .configure(FAIL_ON_UNKNOWN_PROPERTIES, false)
        .configure(FAIL_ON_IGNORED_PROPERTIES, false)
        .configure(USE_BIG_DECIMAL_FOR_FLOATS, true)
        .configure(USE_BIG_INTEGER_FOR_INTS, true)
    )
    

    You can use code similar to above to create your own instance.

    See this thread for some conversation about this topic: https://github.com/http4k/http4k/issues/183