Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
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

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Replace async with this@coroutineScope.async.

There must be some other function in your scope called async that has a higher priority resolution than CoroutineScope.async inside your coroutineScope lambda. The coroutineScope lambda is passed a CoroutineScope as the receiver, so this@coroutineScope.async will clarify that you specifically mean to call CoroutineScope.async on the receiver of the lambda and not some other async function.

I'm not familiar with Spring so I don't have any guesses what the other async function is. You might be able to change your imports in this file such that async will resolve to CoroutineScope.async without you having to prefix it with this@coroutineScope. If you want to find out what other function it is resolving to, remove the prefix and Ctrl+Click the function name async and the IDE will take you to the source code for it.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda