簡體   English   中英

java.net.http 在 JDK11 上不存在錯誤

[英]java.net.http does not exist error on JDK11

它是一個命令行程序。 試圖使用 java.net.http 模塊發送 POST 調用,但我不斷收到以下錯誤。

java: package java.net.http does not exist
C:\Users\dell>java -version
java version "11.0.10" 2021-01-19 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.10+8-LTS-162)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.10+8-LTS-162, mixed mode)

Intellij 項目 SDK - java 版本 11.0.10

代碼 -

import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.URI;

HttpClient client = HttpClient.newHttpClient();

HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://api.serphouse.com/serp/live"))
                .POST(HttpRequest.BodyPublishers.ofString(content))
                .header("Content-Type", "application/json")
                .header("Authorization", "Bearer xZWmYW3fJ0L0juYQrZ6br3etoNhF1iGVhwrWxRlwbhGWflzDeOR0tEPAiJQqJFxc")
                .build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

讓它工作。 剛剛更新了 Intellij。 謝謝@xerx593 和@alain.janinm

我按照 IDE 的建議修復了它,該建議將語言級別轉換為 11,並在 pom.xml 中添加了以下插件

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM