如何使用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编码的例子,谢谢

accept-encoding: gzip, deflate, br

用于http客户端向服务器发出请求并期望得到http响应,该响应为:

content-encoding: gzip

你正在做什么看这个答案:https://serverfault.com/a/56707

最新更新