3

The current Spring Android documentation says in section 2.2.2:

RestTemplate supports sending and receiving data encoded with gzip compression.

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?

clemp6r
  • 3,665
  • 2
  • 26
  • 31

2 Answers2

3

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

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

Community
  • 1
  • 1
Stoozi
  • 114
  • 1
  • 10
  • GZip compression on response is based on the "Content-Encoding" on request is based on the "Accept-Encoding" header. Its need to have headers.setAcceptEncoding(ContentCodingType.GZIP); – sytolk Sep 17 '15 at 12:46
  • Using the Accept-Encoding header value of GZIP in the request indicates to the server that you expect the response to be GZIP encoded. The question was how to **send** GZIP encoded data in the request, which is done by setting the "Content-Encoding" header of the request to GZIP – Stoozi Sep 18 '15 at 12:06
  • If request from client app have Accept-Encoding:gzip header response have from server app will have Content-Encoding:gzip header. RestTemplate is on client side its need to setAcceptEncoding header to have compressed response with Content-Encoding: gzip header from server. Its need to change headers for request/response in your Answer :) – sytolk Sep 18 '15 at 12:29
  • We are in agreement in terms of Gzip encoding the RESPONSE. But this question was specifically around sending Gzip encoded content in the REQUEST, which my original answer provided – Stoozi Sep 18 '15 at 12:32
  • This is my working code using Android RestTemplate Client REQUEST: requestHeaders.setAcceptEncoding(ContentCodingType.GZIP); HttpEntity requestEntity = new HttpEntity(coordinates, requestHeaders); ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.PUT, requestEntity, Integer.class); – sytolk Sep 18 '15 at 15:01
  • You answer is useful I have already marked it. But its have bug if you run this code REQUEST you will not have GZIP response from Rest server :) its need to have headers.setAcceptEncoding instead of headers.setContentEncoding – sytolk Sep 18 '15 at 15:13
  • Gzip data in the response is a separate concern entirely and was not mentioned in the original question so I would not consider my answer to have a bug. Both headers (not one instead of the other) would be required if there was a need for Gzip data in both the request and the response but, again, that wasn't what was asked. – Stoozi Sep 18 '15 at 15:47
0

Just to share my working code for RestTemplate request with AcceptEncoding:gzip

RestTemplate restTemplate = new RestTemplate();  
HttpHeaders requestHeaders = new HttpHeaders();  
requestHeaders.setAcceptEncoding(ContentCodingType.GZIP); 
HttpEntity<Coordinates> requestEntity = new HttpEntity<Coordinates>(coordinates, requestHeaders); 
ResponseEntity<Integer> responseEntity = restTemplate.exchange(url, HttpMethod.PUT, requestEntity, Integer.class);

The source code in answer of @Stoozi not work for me (if you use it simple will not receive compressed response) I have test it with SoapUI

Request:

GET http://localhost:8081/jaxrs/admin-adblock
Accept:application/json
Cache-Control:no-cache
Content-Type:application/json
Authorization:Basic c21h... 
Accept-Encoding:gzip,deflate

Response:

HTTP/1.1 200 OK
Content-Type: application/json
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 204
Server: Jetty(9.2.2.v20140723)

its need to use setAcceptEncoding() instead of setContentEncoding() in RestTemplate REQUEST headers.

sytolk
  • 7,223
  • 3
  • 25
  • 38