![](/img/trans.png)
[英]Spring WebClient: How to convert a HTTP response body to String for a specific encoding
[英]How to convert the new HTTP Client response body to String in Java 9?
當我在Java 9中嘗試使用新的HTTP Client時:
HttpResponse response = HttpRequest
.create(new URI("http://stackoverflow.com/"))
.GET()
.response();
返回的HttpResponse
提供了一個接受BodyProcessor
實現的body
方法。 BodyProcessor
javadoc說:
HttpResponse
中提供了此接口的實現,它將響應寫入String
,byte[]
,File
,Consumer<byte[]>
。 也可以使用自定義實現。
但我找不到那些實現。 如何將響應體轉換為String
? 我應該為它實現BodyProcessor
接口嗎?
javadoc正確地指出實現在HttpResponse
中可用
HttpResponse response = HttpRequest
.create(new URI("http://stackoverflow.com/"))
.GET()
.response();
String body = response.body(HttpResponse.asString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.