Spring boot request response compression

2019-09-16 18:47发布

问题:

I have a spring boot app which produces a json response which is huge, its almost 2MB. I have tried solutions available online by trying to add the following in application.properties file but it didn't work, can someone help?

server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain

This is the method signature if it helps, I am using spring boot version 1.3.3 and testing the service with postman.

 @RequestMapping(value="/getAssetsSMC",method=RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
        public @ResponseBody String getAssetsSMC(@RequestParam(value = "accessKey", required = true) String accessKey) throws IOException
{
  // code 
return gson.toJson(products);
}

回答1:

It may be worth mentioning that the entire compression story happens only if your client says it supports it.

As browsers normally say they support some compression, a guess would be that you might be testing the service with curl. In that case you may want to supply arguments --compressed and/or --tr-encoding. Or the related CURLOPTs (CURLOPT_ACCEPT_ENCODING and/or CURLOPT_TRANSFER_ENCODING) in code.