将 Java 9 HttpClient 代码升级到 Java 11:BodyProcessor 和 asString()
Upgrading Java 9 HttpClient code to Java 11: BodyProcessor and asString()
I have a code base which (apparently) works under Java 9
but does not compile under Java 11
. It uses the jdk.incubator.httpclient
API and changing the module information according to 大部分答案都有效,但不仅仅是包发生了变化。
我仍然无法修复的代码如下:
private static JSONObject sendRequest(JSONObject json) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest httpRequest = HttpRequest.newBuilder(new URI(BASE_URL))
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.timeout(TIMEOUT_DURATION)
.POST(HttpRequest.BodyProcessor.fromString(json.toString()))
.build();
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandler.asString());
String jsonResponse = httpResponse.body();
return new JSONObject(jsonResponse);
}
编译错误为:
Error:(205, 94) java: cannot find symbol
symbol: method asString()
location: interface java.net.http.HttpResponse.BodyHandler
Error:(202, 34) java: cannot find symbol
symbol: variable BodyProcessor
location: class java.net.http.HttpRequest
如何将代码转换为等效的 Java 11
版本?
看起来你需要 HttpResponse.BodyHandlers.ofString()
as a replacement for HttpResponse.BodyHandler.asString()
and HttpRequest.BodyPublishers.ofString(String)
as a replacement for HttpRequest.BodyProcessor.fromString(String)
. (Old Java 9 docs, here。)
你的代码看起来像
private static JSONObject sendRequest(JSONObject json) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest httpRequest = HttpRequest.newBuilder(new URI(BASE_URL))
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.timeout(TIMEOUT_DURATION)
.POST(HttpRequest.BodyPublishers.ofString(json.toString()))
.build();
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
String jsonResponse = httpResponse.body();
return new JSONObject(jsonResponse);
}
I have a code base which (apparently) works under Java 9
but does not compile under Java 11
. It uses the jdk.incubator.httpclient
API and changing the module information according to
我仍然无法修复的代码如下:
private static JSONObject sendRequest(JSONObject json) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest httpRequest = HttpRequest.newBuilder(new URI(BASE_URL))
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.timeout(TIMEOUT_DURATION)
.POST(HttpRequest.BodyProcessor.fromString(json.toString()))
.build();
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandler.asString());
String jsonResponse = httpResponse.body();
return new JSONObject(jsonResponse);
}
编译错误为:
Error:(205, 94) java: cannot find symbol
symbol: method asString()
location: interface java.net.http.HttpResponse.BodyHandler
Error:(202, 34) java: cannot find symbol
symbol: variable BodyProcessor
location: class java.net.http.HttpRequest
如何将代码转换为等效的 Java 11
版本?
看起来你需要 HttpResponse.BodyHandlers.ofString()
as a replacement for HttpResponse.BodyHandler.asString()
and HttpRequest.BodyPublishers.ofString(String)
as a replacement for HttpRequest.BodyProcessor.fromString(String)
. (Old Java 9 docs, here。)
你的代码看起来像
private static JSONObject sendRequest(JSONObject json) throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest httpRequest = HttpRequest.newBuilder(new URI(BASE_URL))
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.timeout(TIMEOUT_DURATION)
.POST(HttpRequest.BodyPublishers.ofString(json.toString()))
.build();
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
String jsonResponse = httpResponse.body();
return new JSONObject(jsonResponse);
}