java - 如何使用 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
推荐阅读
- r - R - 如何格式化持续时间以使用 dplyr 进行正确排序
- android - room db 使用@query 更新多行
- powershell - PowerShell 没有拆分参数
- c# - 如何在 C# 中排除方法以进行 sonarqube 代码覆盖
- mysql - Golang GORM 使用 Lat 和 Lng 查询位置周围的位置
- android - Dragonboard 410c 开机后自动崩溃
- android - Appcelerator Studio - 无法在设备上运行但在 genymotion 模拟器中运行良好?
- ruby-on-rails - UserMailer:Class 的未定义方法“with”
- java - 在硒中拖放时元素会重叠
- c# - 当我通过 Process.Start c# 运行控制台时,控制台关闭