title: What is the Tomcat 10 Jersey 3 way of implementing gzip encoding?
body: <p>I have researched enabling gzip encoding for HTTP responses with Jersey. I have found over a decade of answers that differ a lot. They also seem to focus on Jersey 2 and various servers including Tomcat mostly in version 7. I am using Tomcat 10 and Jersey 3 and I am searching for the solution that makes most sense.</p>
  <p>What already has been partly working for me is adding a WriterInterceptor. The code looks like this:</p>
  <pre><code>@Provider
  public class GZIPWriterInterceptor implements WriterInterceptor {
       
      @Override
      public void aroundWriteTo(WriterInterceptorContext context)
                      throws IOException, WebApplicationException {
          final OutputStream outputStream = context.getOutputStream();
          context.setOutputStream(new GZIPOutputStream(outputStream));
          context.proceed();
      }
  }
  </code></pre>
  <p>You can find similar code in many answers. On the plus side, this code actually encodes the response as gzip. However, the header is not set correctly, so browsers do not decompress the data. Something like <code>Content-Encoding: gzip</code> seems to be missing.</p>
  <p>A different approach seems to be something like <a href="https://stackoverflow.com/a/55559482/15091679">this</a> answer. I tried this:</p>
  <pre><code>public class Base extends ResourceConfig {
      public Base() {
          register(EncodingFilter.class);
          register(GZipEncoder.class);
          register(DeflateEncoder.class);
          property(ServerProperties.OUTBOUND_CONTENT_LENGTH_BUFFER, 32768);
      }
  }
  </code></pre>
  <p>This code is actually executed, I can hit a breakpoint there. I am calling it from web.xml based on <a href="https://stackoverflow.com/questions/45625925/what-exactly-is-the-resourceconfig-class-in-jersey-2">this</a> answer. My web.xml looks like this:</p>
  <pre><code>&lt;web-app version=&quot;5.0&quot; xmlns=&quot;https://jakarta.ee/xml/ns/jakartaee&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;  xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_5_0.xsd&quot;&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;mypackage&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt;
              &lt;param-value&gt;mypackage&lt;/param-value&gt;
          &lt;/init-param&gt;
  
  
          &lt;!--  This was added. --&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;jakarta.ws.rs.Application&lt;/param-name&gt;
              &lt;param-value&gt;mypackage.Base&lt;/param-value&gt;
          &lt;/init-param&gt;
  
  
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;mypackage&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
       &lt;listener&gt;
       &lt;listener-class&gt;mypackage.StartStopListener&lt;/listener-class&gt;
   &lt;/listener&gt;    
  &lt;/web-app&gt;
  </code></pre>
  <p>Side remark: My version differs from the original answer by using jakarta instead of javax.</p>
  <p>However, despite I am using the <a href="https://stackoverflow.com/a/55559482/15091679">answer</a> I have linked, which is accepted, I still do not get gzip encoding with this. Also, I do not really care whether the transfer is chunked or not. I'd rather have it compressed, though.</p>
  <p>So I went after another partial sentence in the answer:</p>
  <blockquote>
  <p>(...) is entirely the container's discretion.</p>
  </blockquote>
  <p>Thus, I concluded that I had to configure it in Tomcat. That lead me <a href="https://examples.javacodegeeks.com/enterprise-java/tomcat/enable-gzip-compression-apache-tomcat/" rel="nofollow noreferrer">here</a>.</p>
  <p>The good news: that actually activated gzip encoding. The minimal code to achieve it needs to be in server.xml:</p>
  <pre><code>&lt;Connector connectionTimeout=&quot;20000&quot; port=&quot;8080&quot; protocol=&quot;HTTP/1.1&quot; redirectPort=&quot;8443&quot; compression=&quot;on&quot;/&gt;
  </code></pre>
  <p>However, I checked and I did not need the Jersey filters to do this. I can remove the ResourceConfig and it still works. I am now completely puzzled. I have the following concrete questions:</p>
  <ol>
  <li>Is the WriterInterceptor a sensible and up-to-date way of compressing the response, and is there a way to modifiy the headers accordingly?</li>
  <li>What are the Jersey filters actually for? I can register them or not, but it does not seem to make a difference.</li>
  <li>The method I found of just letting Tomcat handle the compression seems to be superior to everything else I found. I makes me wonder, though. If that is the solution, why are there so many threads and guides on the web that seem to tell a different story? I do not want to ignore all this information without enough understand to know why I am ignoring it.</li>
  </ol>