Java 9 如何将新的 HTTP Client 响应主体转换为 String?
How to convert the new HTTP Client response body to String in Java 9?
当我在 Java 9 中试验新的 HTTP 客户端时:
HttpResponse response = HttpRequest
.create(new URI("http://whosebug.com/"))
.GET()
.response();
返回的 HttpResponse
提供了一个 body
方法,该方法接受 BodyProcessor
的实现。 BodyProcessor
javadoc 说:
Implementations of this interface are provided in HttpResponse
which
write responses to String
, byte[]
, File
, Consumer<byte[]>
. Custom
implementations can also be used.
但是我找不到那些实现。如何将响应正文转换为 String
?我应该为此实现 BodyProcessor
接口吗?
javadoc 正确指出实现在 HttpResponse
中可用
HttpResponse response = HttpRequest
.create(new URI("http://whosebug.com/"))
.GET()
.response();
String body = response.body(HttpResponse.asString());
当我在 Java 9 中试验新的 HTTP 客户端时:
HttpResponse response = HttpRequest
.create(new URI("http://whosebug.com/"))
.GET()
.response();
返回的 HttpResponse
提供了一个 body
方法,该方法接受 BodyProcessor
的实现。 BodyProcessor
javadoc 说:
Implementations of this interface are provided in
HttpResponse
which write responses toString
,byte[]
,File
,Consumer<byte[]>
. Custom implementations can also be used.
但是我找不到那些实现。如何将响应正文转换为 String
?我应该为此实现 BodyProcessor
接口吗?
javadoc 正确指出实现在 HttpResponse
HttpResponse response = HttpRequest
.create(new URI("http://whosebug.com/"))
.GET()
.response();
String body = response.body(HttpResponse.asString());