java.net.http.HttpClient Kotlin 暂停函数内部:未解决的参考:等待

java.net.http.HttpClient inside Kotlin Suspend Function: Unresolved reference: await

我正在尝试使用 Kotlin 协同程序编写 Http 请求,如

中所述

this guide and that other guide

据我所知,我的代码非常接近这两个示例:

package com.tolearn.service

import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.net.http.HttpResponse.BodyHandlers
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton


@Singleton
class DemoService {

    fun postDemo(key: String, msg: String){

        suspend fun getCoroutine(){
    
            val someData = getData()
    
            print(someData)
        }
    
        suspend fun getData(): String {
    
    
            val client = HttpClient.newBuilder()
                    .version(HttpClient.Version.HTTP_2)
                    .build();
    
            val request = HttpRequest.newBuilder()
                    .uri(URI.create("http://localhost:3000/employees"))
                    .build();
    
            val response = client.sendAsync(request, BodyHandlers.ofString());
    
            return response.await().body() ***here I get Unresolved reference: await
            //return response.get().body() ***this line works but I understand I should prefer use previous line with "awai()" instead of "get()"
        }
    }
}

我的最终目标是公开一个端点(控制器),它将在 Suspend 函数内调用另一个端点 throw java.net.http.HttpClient。我的微服务是完全无状态的,需要尽可能快和轻。这是我第一次编写 Kotlin Coroutine。基于理论学习,我了解到 Coroutine 比 thread 更便宜,我对 Coroutine 做了一个很好的选择。尽管如此,我还是个新手,并且正在为 Error/Exceptions handle 之类的疑问而苦苦挣扎,更直接的是为什么我得到未解决的参考:await

所以我直接的问题是:为什么 return response.await().body() 导致未解决的参考:等待?我应该用 response.get().body() 代替吗?

任何其他建议或提示将不胜感激。

*** 编辑

这是我的 build.gradle。我刚刚添加了 kotlinx-coroutines-jdk8:1.4.2

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.4.10"
    id("org.jetbrains.kotlin.kapt") version "1.4.10"
    id("org.jetbrains.kotlin.plugin.allopen") version "1.4.10"
    id("com.github.johnrengelman.shadow") version "6.1.0"
    id("io.micronaut.application") version "1.2.0"
    id("com.google.protobuf") version "0.8.13"
}

version = "0.1"
group = "com.tolearn"

repositories {
    mavenLocal()
    jcenter()
    mavenCentral()
}

micronaut {
    testRuntime("junit5")
    processing {
        incremental(true)
        annotations("com.tolearn.*")
    }
}

dependencies {
    implementation("io.micronaut:micronaut-validation")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
    implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
    implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
    implementation("io.micronaut:micronaut-runtime")
    implementation("io.micronaut.grpc:micronaut-grpc-runtime")
    implementation("javax.annotation:javax.annotation-api")
    implementation("io.micronaut.kafka:micronaut-kafka")

    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.4.2")

    runtimeOnly("ch.qos.logback:logback-classic")
    runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
    testImplementation("io.micronaut:micronaut-http-client")
}


application {
    mainClass.set("com.tolearn.ApplicationKt")
}

java {
    sourceCompatibility = JavaVersion.toVersion("11")
}

tasks {
    compileKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }
    compileTestKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }


}

sourceSets {
    main {
        java {
            srcDirs("build/generated/source/proto/main/grpc")
            //srcDirs 'build/generated/source/proto/main/grpckt'
            srcDirs("build/generated/source/proto/main/java")
        }
    }
}

protobuf {
    protoc { artifact = "com.google.protobuf:protoc:3.14.0" }
    plugins {
        grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.33.1" }
        //grpckt { artifact = "io.grpc:protoc-gen-grpc-kotlin:1.0.0" }
    }
    generateProtoTasks {
        all()*.plugins {
            grpc {}
            //grpckt {}
        }
    }
}

确保您的类路径中有 kotlinx-coroutines-jdk8 库,然后使用以下导入语句:

import kotlinx.coroutines.future.await