Dmytro Koval
01/09/2024, 1:26 PMinline reified
functions. I’m making a generic web client using Spring WebFlux. For example a generic GET fun looks like this:
suspend inline fun <reified T> httpGet(uri: String, params: Map<String, Any>): T? {
return webClient.get()
.uri(uri, params)
.exchangeToMono { response ->
when (response.statusCode()) {
HttpStatus.OK -> response.bodyToMono(T::class.java)
HttpStatus.NOT_FOUND -> Mono.empty()
HttpStatus.BAD_REQUEST ->
response.bodyToMono(ErrorResponse::class.java)
.flatMap { errorResponse: ErrorResponse ->
Mono.error(mapToException(errorResponse))
}
else -> response.createException().flatMap { Mono.error(it) }
}
}
.timeout(requestTimeout)
.retryWhen(
retryPolicy
.onRetryExhaustedThrow { _, retrySignal ->
RuntimeException("Failed to fetch $uri", retrySignal.failure())
},
)
.awaitFirstOrNull()
}
This works great until this function is called within a coroutine scope. In that case T
seems to not be reified anymore. Am I doing something wrong or is it a bug?Dmytro Koval
01/09/2024, 1:45 PMT
is a collection. Let’s say I call it like this:
client.httpGet<List<Promotion>>(promotionsPath, mapOf("contactId" to contactId.value))
Promotion
object in this case won’t be reifiedSam
01/09/2024, 3:07 PMT::class.java
will produce a Class
instance that doesn't preserve any generic information. Does it work better if you use the Kotlin-specific bodyToMono<T>()
function? https://docs.spring.io/spring-framework/docs/current/kdoc-api/spring-webflux/org.springframework.web.reactive.function.client/body-to-mono.htmlDmytro Koval
01/10/2024, 11:13 AMbodyToMono<T>()
a try