如何使用 RestTemplate 发送 Gzip 请求?

问题描述

我有以下带有大 pojo (MyRequest) 的代码,我想用 Gzip 发送它,但它无法到达接受 Gzip 请求的端点。我是否正确创建了 Gzip 请求?我需要将 pojo 作为文件发送吗?

MyRequest request = new MyRequest ();

HttpHeaders httpHeaders = HttpHeaders();
httpHeaders.add(HttpHeaders.CONTENT_ENCODING,"gzip");
httpHeaders.add(HttpHeaders.ACCEPT_ENCODING,"gzip");
httpHeaders.add(HttpHeaders.CONTENT_TYPE,"gzip");

HttpEntity<byte[]> entity = new HttpEntity<>(compress(request),headers);
ResponseEntity<MyResponse> response = restTemplate.postForEntity(url,entity,MyResponse.class);



public static Byte[] compress(byte[] body) throws IOException {
  
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(baos)) {
    gzipOutputStream.write(body);
  }
  
  return baos.toByteArray();
}

请分享一个使用 RestTemplate 的 Gzip 编码示例,谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)