2

Is there a way to add Gzip for one method or controller in spring

@RequestMapping(value = "/system", method = {RequestMethod.GET})
@Gzip //<- Something similar this,
public ApiResponse status() throws Exception{

}

I dont want to enable it for the entire server using tomcat configuration, since my clients are not yet ready for consuming gzip,

2
  • stackoverflow.com/questions/16638345/… Commented Sep 13, 2017 at 7:51
  • But these all examples are explaining to gzip at filter level, I am looking to have the decision at my controller or controller method level Commented Sep 13, 2017 at 7:57

1 Answer 1

1

You can put java.io.OutputStream or javax.servlet.http.HttpServletResponse (for specific gzip HTTP headers) to your Controller method as parameter wrapping it with java.util.zip.GZIPOutputStream before writing the content to the client

@RequestMapping(value = "/system", method = {RequestMethod.GET})
public void status(HttpServletResponse response) throws Exception {
   try(GZIPOutputStream out = new GZIPOutputStream(response.getOutputStream())) {
       // write to out the content
   }
}
Sign up to request clarification or add additional context in comments.

Comments

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.