2

I need to read gzipped json payload in a POST request in my SPringBoot app which accepts json data. How to do that in order to keep the application generic as there may be other clients in future sending data in plain json or other compression formats? I suppose this should be handled by the server itself so is there any way to instruct the embedded Tomcat to unzip the payload?

My SpringBoot application runs on embedded Tomcat 9.0.17.

The controller accepts JSON payload in a POST request.

@RequestMapping(value = "/update/v1", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
    public ResponseEntity<String> receiveUpdates(@RequestBody String update) {

We recently changed our content provider and the new one is sending payload in "gzip" format (with header content-encoding=gzip) and without any content-type header. As a result it gives the following error

'error': 'Unsupported Media Type', 'message': "Content type '' not supported"

If I change my consume type to MediaType.ALL_VALUE, my controller starts receiving the request but the payload itself is gzipped. I can handle it in my service layer but that would make it specific to gzipped data.

This problem could be solved by introducing a Filter to handle gzipped payload as mentioned here and here.

But I believe there should be a way to instruct the Tomcat to handle this and serve unzipped data.

ankit3j
  • 51
  • 2
  • 8
  • You could probably do it at tomcat level ( not sure ) - a google search reveals millions of answers, literally; but why not do it via a filter? – Eugene Jul 31 '19 at 10:17
  • Hey Eugene, if nothing else then I'll have to do it via filter but if the server can handle out-of-the-box then it would save some code complexity. I don't know about millions but couldn't find any answer on Google. Whatever I found was either about standalone servers and not embedded ones or they were simply about compression of response. Can you direct me to some useful link? – ankit3j Jul 31 '19 at 11:45
  • sorry not millions indeed, but when I search `tomcat gzip` I get `586.000` results. none of those can be helpful to you? – Eugene Jul 31 '19 at 14:02
  • This answer seems to provide filter code that can be used in spring boot to keep the application generic. https://stackoverflow.com/questions/20507007/http-request-compression/29096025#29096025 – codebrane Jul 31 '19 at 14:42
  • 1
    @Eugene, these results are mostly about compressing API response. – ankit3j Aug 01 '19 at 05:35
  • @codebrane, ya that seems to be the only solution now, I mentioned that link in my question though. – ankit3j Aug 01 '19 at 05:36

0 Answers0