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,

Ysak
  • 2,601
  • 6
  • 29
  • 53
  • https://stackoverflow.com/questions/16638345/how-to-decode-gzip-compressed-request-body-in-spring-mvc – StanislavL Sep 13 '17 at 07: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 – Ysak Sep 13 '17 at 07:57

1 Answers1

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
   }
}
alex.b
  • 1,448
  • 19
  • 23