'question on kotlin coroutines with webclient
I am new to kotlin. I have written a function that processes two webclient calls in parallel and then aggregates the result. the point is that it is written in a java style (below the code)
@GetMapping("/personv1/{id}")
suspend fun getPerson1(@PathVariable id :Integer): Mono<Person> =
Mono.zip(
webClient.get().uri("/person/{id}", id).retrieve().bodyToMono(Person::class.java),
webClient.get().uri("/person/{id}/domain", id).retrieve().bodyToMono(String::class.java)
) { person, domain -> Person(person.id, person.name, domain) }
I wanted to make the same code using coroutines following examples, but I am blocked at the very beginning. My idea was to declare two variables one of type Deferred of a Person and the other one Deferred of a String and then wait for them to merge the data but I am not able to declare any of the variables.
Here is the code I have started
@GetMapping("/personv2/{id}")
suspend fun getPerson2(@PathVariable id :Integer): Person = coroutineScope {
val person: Deferred<Person> = async {
webClient
.get()
.uri("/person/{id}", id)
.retrieve()
.awaitBody<Person>()
}
}
But my IDE is telling me that my person val should be of type ServerResponse and not a Deferred of Person
Can someone help me on that ? what am I doing wrong?
thanks for your help
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|