Hello. Is there a way to make the HttpClient JsonF...
# ktor
a
Hello. Is there a way to make the HttpClient JsonFeature reuse the same serializer/mapper we have configured for the server?
d
Share the code that creates the mapper between both modules using a shared module or copy it
a
so here we create a common mapper to be used by our application:
Copy code
val mapper: ObjectMapper = jacksonObjectMapper()
    .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
    .setSerializationInclusion(JsonInclude.Include.NON_NULL)
then we set it as a part of content negotiation feature
Copy code
install(ContentNegotiation) {
        register(ContentType.Application.Json, JacksonConverter(mapper))
    }
however, we are unable to use the same mapper for HttpClient
Copy code
val defaultHttpClient = HttpClient(OkHttp) {
        expectSuccess = false
        install(JsonFeature) {
            this.serializer = JacksonSerializer {
                configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                setSerializationInclusion(JsonInclude.Include.NON_NULL)
            }
        }
        engine {
            config {
                readTimeout(Duration.ofMillis(2000))
                connectTimeout(Duration.ofMillis(5000))
                connectionPool(ConnectionPool(100, 1, TimeUnit.MINUTES))
            }
        }
    }
would be nice to be able to pass the
mapper
as well
d
Have a function that configures
ObjectMapper
instead
function ObjectMapper.configureContentNegotiationFeatures() { setSerializationInclusion(...) }
And call it from server and client
a
thank dico. yeah we had to do something like this. would be convenient though if mapper can just be passed