https://kotlinlang.org logo
#ktor
Title
# ktor
a

anthony castillo

01/14/2020, 12:11 AM
Hello. Is there a way to make the HttpClient JsonFeature reuse the same serializer/mapper we have configured for the server?
d

Dico

01/14/2020, 12:51 AM
Share the code that creates the mapper between both modules using a shared module or copy it
a

anthony castillo

01/14/2020, 1:46 AM
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

Dico

01/14/2020, 10:09 AM
Have a function that configures
ObjectMapper
instead
function ObjectMapper.configureContentNegotiationFeatures() { setSerializationInclusion(...) }
And call it from server and client
a

anthony castillo

02/04/2020, 2:03 AM
thank dico. yeah we had to do something like this. would be convenient though if mapper can just be passed
3 Views