rrva
09/02/2020, 10:00 AMaraqnid
09/02/2020, 7:54 PMrrva
09/02/2020, 9:33 PMclient.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply { it.body() }
.thenApply { deserialize(it) }
.await()
Meaning that reading the body and deserializing (using jackson objectmapper) is done in java world on the executor used by HttpClient, and then there is a switch to a coroutinerrva
09/02/2020, 9:34 PMrrva
09/03/2020, 9:15 AMaraqnid
09/03/2020, 9:16 AMrrva
09/03/2020, 9:17 AMclass JsonBodyHandler<W>(private val wClass: Class<W>) : BodyHandler<Supplier<W>> {
override fun apply(responseInfo: ResponseInfo): BodySubscriber<Supplier<W>> {
return asJSON(wClass)
}
}
fun <W> asJSON(targetType: Class<W>?): BodySubscriber<Supplier<W>> {
val upstream = BodySubscribers.ofInputStream()
return BodySubscribers.mapping(upstream) { inputStream: InputStream -> toSupplierOfType(inputStream, targetType) }
}
private fun <W> toSupplierOfType(inputStream: InputStream, targetType: Class<W>?): Supplier<W> {
return Supplier {
try {
inputStream.use { stream ->
val objectMapper = createObjectMapper()
objectMapper.readValue(stream, targetType)
}
} catch (e: IOException) {
throw UncheckedIOException(e)
}
}
}
adapted from https://stackoverflow.com/questions/57629401/deserializing-json-using-java-11-httpclient-and-custom-bodyhandler-with-jacksonaraqnid
09/03/2020, 9:17 AMaraqnid
09/03/2020, 9:19 AMaraqnid
09/03/2020, 9:21 AMrrva
09/03/2020, 9:21 AMaraqnid
09/03/2020, 9:25 AMaraqnid
09/03/2020, 9:27 AMrrva
09/03/2020, 9:36 AMrrva
09/03/2020, 9:39 AMaraqnid
09/03/2020, 9:45 AMrrva
09/03/2020, 9:50 AMrrva
09/03/2020, 9:50 AMrrva
09/03/2020, 9:51 AM