alex
11/06/2019, 11:07 PMClassCastException
and debugging shows that line no. 12
does nothing.
What might be the reason, that inline
version sometimes (for some calls) skips the cast and returns the same object (i.e. String
) instead of reified T
?
Classic approach with passing class ref works fine.fun getModel(id: Int: CompletableFuture<Model> {
val request: HttpRequest = //...
return sendAsync(request)
}
Quy D X Nguyen
11/07/2019, 3:33 AMalex
11/07/2019, 8:33 AMQuy D X Nguyen
11/07/2019, 1:40 PMalex
11/07/2019, 3:28 PMprotected inline fun <reified T> sendAsync(
request: HttpRequest,
typeReference: TypeReference<T> = jacksonTypeRef()
): CompletableFuture<T> {
return client.sendAsync(request) { _ ->
BodySubscribers.mapping(BodySubscribers.ofString(StandardCharsets.UTF_8)) {
mapper.readValue(it, typeReference)
}
}.thenApply { it.body() }
}
looks the same, but instead of simple reified T
, I'm handing over TypeReference
as well.