0

I have a spring project where I need to compress the JSON response if the size of the response is greater that 2KB. Spring has the property server.compression.min-response-size=2048 but is only does it for response of type String. Recommendation from spring team as below

  • 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.*

What is the best way to achieve this if performance impact is not taken into consideration?

3
  • Create filter, read response, put in byte array, after wards write to actual response including setting the content-length header. This will really impact performance and if you have large payloads could even lead to memory issues. Commented Dec 3, 2024 at 11:21
  • @M.Deinum how do i write the response to byte array. I tried creating a filter but the object HttpServletResponse does not have any method to get the response in byte array. Commented Dec 4, 2024 at 4:59
  • It has an outputstream, you would need to write a wrapper for the response that caches it instead of directly writing it. Commented Dec 4, 2024 at 6:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.