I am trying to implement Gzip response compression in my Springboot Rest API
I am using below configuration in my application.properties
Enable response compression:
server.compression.enabled=true
The comma-separated list of mime types that should be compressed:
server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript,application/json
Compress the response only if the response size is at least 10KB:
server.compression.min-response-size=10240
Response compression is happening, but the strange thing is that it is also compression responses having size as low as 1KB or 500B which it should not as server.compression.min-response-size=10240
Comment From: wilkinsona
Thanks for the report. We have tests that verify that small response are not compressed so, based on the information provided, it's hard to say why things are behaving the way they are for you. Do your responses have a Content-Length header? If not, they will be compressed regardless of their size. If this does not help, please create a minimal sample that reproduces the problem and share it with us as a zip attached to this issue or in a separate repository on GitHub.
Comment From: dineshv1234567
@wilkinsona Thanks for the reply. As per security policy, I can not share the data but it has a Content-length header in the response. Its value is 3.71 KB (body: 3.51KB, headers: 201b) Spring Boot Version used = 2.1.7-RELEASE But in application.properties:
server.compression.min-response-size=61440000
I think this might help to reproduce the issue: https://javadeveloperzone.com/spring-boot/spring-boot-compress-response/
Comment From: wilkinsona
We don't need your exact code, just a small sample that reproduces the problem that you are seeing. As I said above, we have tests that verify that small responses are not compressed so I suspect it is something specific to what you are doing that is causing the problem or exposing a bug. Without knowing exactly what you're doing we're unlikely to be able to identify the problem. If you would like us to spend some more time investigating, please take the time to create a small sample that reproduces the specific problem that you are seeing.
Comment From: dineshv1234567
Can you tell me how can I check its server.compression.min-response-size at runtime? Maybe its default value is not changing
Comment From: wilkinsona
How it's set in the embedded container depends on which embedded container you're using. This is one of the things that a minimal sample will answer for us. Can you please provide one as requested above?
Comment From: dineshv1234567
I am running as a java application in eclipse IDE and testing via Postman
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.
Comment From: mayuresh-chandwadkar
I also observed that below property is not working. Even my API returning repose less than 100KB it is compressing the response. server.compression.min-response-size=102400
Comment From: sunilkumar065
Hi @wilkinsona I am able to reproduce this issue. Attaching a sample application. Min response size is honored when the return type of the API is string, but not being honored when the return type of API is JSON. Also content length header is populated only for non compressed string response. Is there any way to add content length of the reponse from ServletOutputStream. I could not find a way for that. demo 2.zip
Comment From: bclozel
@sunilkumar065 you're describing the expected behavior. The minimum size is only honored if the size is known before the body is written, thanks to the "Content-Length" response header. If you really want to honor this is all cases, you would need to buffer the entire JSON response body and then write it at once; this is likely to make performance worse.
Comment From: sunilkumar065
Thanks you for the response @bclozel . Do you have any recommendation on how to achieve this for JSON responses?
Comment From: bclozel
Yes, I would recommend asking this question on StackOverflow to get help from the community.