So basically im trying to get a gzip encoded json response to a pojo in java by trying to decompress the gzip. At first im getting the response in byte array form from the api call.
CategoriesFullDetails categoriesFullDetails = new CategoriesFullDetails();
UriComponents getAllCategoriesUri = UriComponentsBuilder
.fromHttpUrl(baseUrl + MENU_CATEGORY_FULL)
.buildAndExpand(businessId);
String getAllCategoriesUrl = getAllCategoriesUri.toUriString();
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Content-Type", "application/json");
requestHeaders.set("Accept-Encoding", "gzip");
HttpEntity httpEntity = new HttpEntity(requestHeaders);
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
client.setRequestFactory(requestFactory);
client.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
byte[] responseBytes = client
.exchange(getAllCategoriesUrl, HttpMethod.GET, httpEntity, byte[].class).getBody();
Once i get the gzip response converted and stored as a byte array as shown above, i want to decompress it and add it to a pojo of mine which is CategoriesFullDetails.
Below im calling the method which is going to decompress the byte array.
try {
categoriesFullDetails = decompress(responseBytes);
return categoriesFullDetails;
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
return null;
}
public CategoriesFullDetails decompress(byte[] data) throws IOException, ClassNotFoundException {
ByteArrayInputStream in = new ByteArrayInputStream(data);
GZIPInputStream gis = new GZIPInputStream(in);
ObjectInputStream is = new ObjectInputStream(gis);
return (CategoriesFullDetails) is.readObject();
}
So what i found out when debugging this decompress method was, it converts the data to a ByteArrayInputStream and then to a GZIPInputStream successfully (first 2 lines of the method works fine). But then error is thrown at ObjectInputStream is = new ObjectInputStream(gis); saying StreamCorruptedException: invalid stream header: 7B227061
I hope someone can help me and fix this, its been 3 days and still i cant solve it.