使用Spring Android RestTemplate发送Gzip压缩数据? [英] Sending Gzip compressed data with Spring Android RestTemplate?

查看:215
本文介绍了使用Spring Android RestTemplate发送Gzip压缩数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前 Spring Android文档在第2.2.2节:

The current Spring Android documentation says in section 2.2.2:

RestTemplate支持发送和接收使用gzip压缩编码的数据.

RestTemplate supports sending and receiving data encoded with gzip compression.

但是,本文档在2.7.2节中说明了如何接收Gzip数据,但是与发送gzip数据(使用POST或PUT)无关.它是缺少的功能,所以介绍会是错误的吗?还是有一些秘密的方法来启用gzip压缩?

However, this document explains in section 2.7.2 how to receive Gzip data, but there is nothing about sending gzip data (using a POST or a PUT). Is it a missing feature so the introduction would be erroneous? Or is there some secret way to enable gzip compression?

推荐答案

对请求的GZip压缩基于由RestTemplate处理的请求的"Content-Encoding"标头.将此标头设置为"gzip"将为您的请求启用Gzip压缩.幸运的是,有一些常量和辅助函数可简化此操作:

GZip compression on requests is based on the "Content-Encoding" header of the request being handled by the RestTemplate. Setting this header to "gzip" will enable Gzip compression for your request. Luckily there are some constants and helper functions available to make this easy:

HttpHeaders headers = new HttpHeaders();
headers.setContentEncoding(ContentCodingType.GZIP);
//...then use headers when making request with RestTemplate instance

使用启用了Gzip压缩的ClientHttpRequestInterceptor时要小心,因为这将多次压缩您的请求正文(取决于您配置的拦截器的数量),如我在此处所述: 带有ClientHttpRequestInterceptor的RestTemplate会导致GZIP压缩两次

Be wary when using a ClientHttpRequestInterceptor with Gzip compression enabled as this will compress your request body multiple times (depending on how many interceptors you have configured) as I describe here: RestTemplate with ClientHttpRequestInterceptor causes GZIP compression twice

这篇关于使用Spring Android RestTemplate发送Gzip压缩数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆