我正在使用 Spring WebClient (Spring 5.1.3) 调用 Web 服务。服务响应content-type: application/json
和content-encoding: gzip
ClientResponse.bodyToMono
然后失败并出现错误“JSON 解码错误:非法字符((CTRL-CHAR,代码 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 个标头:
Accept: application/json
Accept-Encoding: gzip
响应具有以下标头:
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))