使用 Spring WebClient 解码 content-encoding gzip
Decode content-encoding gzip using Spring WebClient
我正在使用 Spring WebClient (Spring 5.1.3) 调用 Web 服务。服务以 content-type: application/json
和 content-encoding: gzip
响应
ClientResponse.bodyToMono
然后失败并出现错误 "JSON decoding error: Illegal character ((CTRL-CHAR, code 31))",我认为这是因为在尝试解析 JSON.
之前内容尚未解码
这是我如何创建 WebClient 的代码片段(简化)
HttpClient httpClient = HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext));
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)).build();
然后我使用 WebClient 进行调用:
webClient.get().uri(uri)
.accept(MediaType.APPLICATION_JSON)
.header(HttpHeaders.ACCEPT_ENCODING, "gzip")
.exchange()
HTTP请求有2个headers:
Accept: application/json
Accept-Encoding: gzip
响应具有以下内容headers:
set-cookie: xxx
content-type: application/json; charset=utf-8
content-length: 1175
content-encoding: gzip
cache-control: no-store, no-cache
通过执行以下操作,我能够手动解码 GZIP 内容并从结果
中获得有效 JSON
webClient.get().uri(uri)
.accept(MediaType.APPLICATION_JSON)
.header("accept-encoding", "gzip")
.exchange()
.flatMap(encodedResponse -> encodedResponse.body((inputMessage, context) ->
inputMessage.getBody().flatMap(dataBuffer -> {
ClientResponse.Builder decodedResponse = ClientResponse.from(encodedResponse);
try {
GZIPInputStream gz = new GZIPInputStream(dataBuffer.asInputStream());
decodedResponse.body(new String(gz.readAllBytes()));
} catch (IOException e) {
e.printStackTrace();
}
decodedResponse.headers(headers -> {
headers.remove("content-encoding");
});
return Mono.just(decodedResponse.build());
}).flatMap(clientResponse -> clientResponse.bodyToMono(Map.class))
reactor netty 客户端本身支持此功能。
您应该像这样创建 HttpClient
:
HttpClient httpClient = HttpClient.create()
.secure(sslContextSpec -> sslContextSpec.sslContext(sslContext))
.compress(true);
然后无需添加接受编码请求header,因为它已为您完成。
请注意,当您不提供自定义 HttpClient
实例时,this bit is done by the connector itself。
我正在使用 Spring WebClient (Spring 5.1.3) 调用 Web 服务。服务以 content-type: application/json
和 content-encoding: gzip
ClientResponse.bodyToMono
然后失败并出现错误 "JSON decoding error: Illegal character ((CTRL-CHAR, code 31))",我认为这是因为在尝试解析 JSON.
这是我如何创建 WebClient 的代码片段(简化)
HttpClient httpClient = HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext));
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)).build();
然后我使用 WebClient 进行调用:
webClient.get().uri(uri)
.accept(MediaType.APPLICATION_JSON)
.header(HttpHeaders.ACCEPT_ENCODING, "gzip")
.exchange()
HTTP请求有2个headers:
Accept: application/json
Accept-Encoding: gzip
响应具有以下内容headers:
set-cookie: xxx
content-type: application/json; charset=utf-8
content-length: 1175
content-encoding: gzip
cache-control: no-store, no-cache
通过执行以下操作,我能够手动解码 GZIP 内容并从结果
中获得有效 JSONwebClient.get().uri(uri)
.accept(MediaType.APPLICATION_JSON)
.header("accept-encoding", "gzip")
.exchange()
.flatMap(encodedResponse -> encodedResponse.body((inputMessage, context) ->
inputMessage.getBody().flatMap(dataBuffer -> {
ClientResponse.Builder decodedResponse = ClientResponse.from(encodedResponse);
try {
GZIPInputStream gz = new GZIPInputStream(dataBuffer.asInputStream());
decodedResponse.body(new String(gz.readAllBytes()));
} catch (IOException e) {
e.printStackTrace();
}
decodedResponse.headers(headers -> {
headers.remove("content-encoding");
});
return Mono.just(decodedResponse.build());
}).flatMap(clientResponse -> clientResponse.bodyToMono(Map.class))
reactor netty 客户端本身支持此功能。
您应该像这样创建 HttpClient
:
HttpClient httpClient = HttpClient.create()
.secure(sslContextSpec -> sslContextSpec.sslContext(sslContext))
.compress(true);
然后无需添加接受编码请求header,因为它已为您完成。
请注意,当您不提供自定义 HttpClient
实例时,this bit is done by the connector itself。