Kotlin HttpClient 和 HttpRequest 在 Intelij-idea 上出现未解决的引用错误。

huangapple go评论54阅读模式
英文:

Kotlin HttpClient and HttpRequest unresolved reference errors on Intelij-idea

问题

以下是代码部分的中文翻译:

我在我的 Kotlin 代码中无法正确导入或映射 HttpClient 和 HttpRequest 的引用我对 Kotlin 还很陌生所以可能没有正确配置项目我在 Mac 和 Windows 机器上都尝试过都遇到了相同的问题

我的导入语句

import io.jsonwebtoken.Header
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.io.Decoders
import io.jsonwebtoken.security.Keys
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.security.Key
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.util.*
import kotlin.collections.HashMap

在主函数中引起问题的代码行

val client = HttpClient.newBuilder().build();
val request = HttpRequest.newBuilder()
    .uri(URI.create("https://openapi.example.com/example/"))
    .header("Authorization", "Bearer $jwt")
    .header("Content-Type", "application/json")
    .POST(HttpRequest.BodyPublishers.ofString(body))
    .build();

我收到了未解决的引用错误

注意:上述翻译中,HTML 实体编码(如")已被还原为正常的双引号。

英文:

I can't get references to HttpClient and HttpRequest to import/map properly in my Kotlin code. I'm new to Kotlin, so I might not being configuring the project correctly. I tried this both on Mac and Windows machines, and ran into the same issue.

My Import statements:

import io.jsonwebtoken.Header
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.io.Decoders
import io.jsonwebtoken.security.Keys
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.security.Key
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.util.*
import kotlin.collections.HashMap

Lines causing issues in inside the main fuction:

val client = HttpClient.newBuilder().build();
val request = HttpRequest.newBuilder()
    .uri(URI.create("https://openapi.example.com/example/"))
    .header("Authorization", "Bearer $jwt")
    .header("Content-Type", "application/json")
    .POST(HttpRequest.BodyPublishers.ofString(body))
    .build();

I receive unresolved reference errors:

Kotlin HttpClient 和 HttpRequest 在 Intelij-idea 上出现未解决的引用错误。

Configuration:

  • Intelij-IDEA with Kotlin (latest)
  • Amazon Corretto 17
  • Gradle 8.0.1

答案1

得分: 1

尝试打开您的 build.gradle/build.gradle.kts 文件,并将 jvmToolchain 更改为 17。在 IDEA 中重新加载 Gradle。

英文:

The import java.net.http.HttpClient was added in JDK 11 but your Gradle configuration should still uses a lower language level.

Try to open your build.gradle/build.gradle.kts, and change your jvmToolchain to 17. Reload the Gradle in IDEA.

huangapple
  • 本文由 发表于 2023年2月24日 06:05:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定