How to gzip HTTP request, created by org.springframework.web.client.RestTemplate?
I am using Spring 4.2.6
with Spring
Further to the above answer from @TestoTestini, if we take advantage of Java 7+'s 'try-with-resources' syntax (since both ByteArrayOutputStream
and GZIPOutputStream
implement closeable() ) then we can shrink the getGzip function into the following:
private byte[] getGzip(byte[] body) throws IOException {
try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
GZIPOutputStream zipStream = new GZIPOutputStream(byteStream)) {
zipStream.write(body);
byte[] compressedData = byteStream.toByteArray();
return compressedData;
}
}
(I couldn't find a way of commenting on @TestoTestini's original answer and retaining the above code format, hence this Answer).