0

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • Why are you converting `GZIPInputStream` to `ObjectInputStream`? From the _javadoc_ of class `ObjectInputStream`: _An ObjectInputStream deserializes primitive data and objects previouslywritten using an ObjectOutputStream_ I think you need to just decompress the `GZIPInputStream`. There is no need for the conversion to `ObjectInputStream`. – Abra Oct 17 '20 at 07:27
  • @Abra Can u pls tell me how to decompress the GZIPInputStream, it would be really helpful to me, and btw if i convert it to a ObjectInputStream, then i will be able to add it to my pojo like in the next line. – Yeshan Santhush Oct 17 '20 at 07:33
  • But if it wasn't produced by an `ObjectOutputStream` you can't do that. – user207421 Oct 17 '20 at 08:33

1 Answers1

1

7B227061 is the hex equivalent of this ASCII:

{"pa

I.e. it looks like the first 4 bytes of JSON data. The issue is that you have a gzipped stream of JSON text and you're passing it to an ObjectInputStream, which is for reading serialised Java object data.

Just pass the GzipObjectStream to your JSON parser. Or, read the entire input stream into a string, if you prefer, and then pass the string to your parser.

E.g., if you're using Jackson, then:

ObjectMapper mapper = new ObjectMapper();
CategoriesFullDetails jsonMap = mapper.readValue(gis, CategoriesFullDetails.class);
jon hanson
  • 8,722
  • 2
  • 37
  • 61
  • Hi, the thing is i cannot add is yet because the error is thrown at " ObjectInputStream is = new ObjectInputStream(gis); " , so... in that caase how? And btw i also tried to get the response of the api to a string and it doesnt work like that, do u mean i should add the "gis" rather than the "is" inside the " mapper.readValue(gis, CategoriesFullDetails.class); ", Pls help me – Yeshan Santhush Oct 17 '20 at 07:56
  • Yes, i've corrected my answer. – jon hanson Oct 17 '20 at 07:59
  • Hey man thnx a lot dude, it works well, i was struggling for 3 days straight tryna figure out this. Means a lot to me dude, appreciate ur help :) – Yeshan Santhush Oct 17 '20 at 09:15