Search code examples
jettyjetty-9

Apply GZipHandler in Jetty for specific paths


How to include only specific paths that match a pattern for GZipHandler in Jetty 9.4. For example, I have a request mapping to path /api/employees/{id}/payments. I need to apply GZip compression only to this path. Jetty only supports exact match , or prefix (/*) or suffix (*.) in include paths. Can I configure GZipHandler to compress only requests to path /api/employees/{id}/payments like /api/employees/1/payments, /api/employees/42/payments etc?


Solution

  • The Jetty GzipHandler supports standard Servlet URL-Pattern mappings.

    Your "request mapping" of /api/employees/{id}/payments is not a valid servlet url-pattern string.

    It looks like a REST API string, which is done entirely within your REST library, without the servlet spec or servlet api really being involved.

    Instead of attempting to control GzipHandler from paths (include / exclude), consider instead setting up the Mime-Type mappings that it should respond to instead. That way you can setup the include path to /api/* and have the mime-types includes decide what content should be compressed or not. Keep in mind that GzipHandler will not compress if the http client has not indicated that it can handle gzip (it checks the Accept and Accept-Encoding request headers), so it would be safe to cast a larger net.

    Also consider setting the minimum size configuration setMinGzipSize(int) so that tiny/small responses do not get needlessly gzipped.