【问题标题】:Decode content-encoding gzip using Spring WebClient使用 Spring WebClient 解码内容编码 gzip
【发布时间】:2019-05-25 04:03:20
【问题描述】:

我正在使用 Spring WebClient (Spring 5.1.3) 调用 Web 服务。该服务以content-type: application/jsoncontent-encoding: gzip 响应

ClientResponse.bodyToMono 然后失败并出现错误“JSON 解码错误:非法字符((CTRL-CHAR,代码 31))”,我认为这是因为在尝试解析 JSON 之前内容尚未被解码。

这里是我如何创建 WebClient 的代码 sn-p(简化)

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))

【问题讨论】:

  • 您可以发布 JSON 吗?
  • reactor netty 客户端默认支持 gzip 解码。您能否发布代码 sn-ps 显示如何创建网络客户端?您能否同时显示 HTTP 请求和响应标头和正文?
  • @BrianClozel 我添加了一些代码 sn-ps。我现在已经能够成功地手动解码内容,但它似乎仍然无法自动处理这个

标签: spring spring-webflux reactor-netty


【解决方案1】:

reactor netty 客户端原生支持此功能。

你应该像这样创建HttpClient

HttpClient httpClient = HttpClient.create()
             .secure(sslContextSpec -> sslContextSpec.sslContext(sslContext))
             .compress(true);

然后不需要添加接受编码请求标头,因为它已经为您完成了。

请注意,this bit is done by the connector itself 在您不提供自定义 HttpClient 实例时。

【讨论】:

  • 感谢@Brian,它现在可以在添加 compress(true) 后工作。我能问一下为什么这是必要的,为什么它不能被内容编码头检测到并自动解码?
  • reactor Netty 客户端默认没有设置所需的解压支持。它需要一个额外的 Netty 处理程序来完成这项工作。
猜你喜欢
  • 2015-01-17
  • 2018-02-27
  • 2017-03-16
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多