Question

Hello i Use jetty 9 with guice and jersey. How to dorce use GZIP?

Server server = new Server(8080);
ServletContextHandler handler = new ServletContextHandler();
handler.addFilter(GuiceFilter.class, "/*", EnumSet.allOf(DispatcherType.class));
handler.addFilter(CORSFilter.class, "/*", EnumSet.allOf(DispatcherType.class));

FilterHolder holder = new FilterHolder(GzipFilter.class);
holder.setInitParameter("deflateCompressionLevel", "9");
holder.setInitParameter("minGzipSize", "0");
holder.setInitParameter("mimeTypes", "application/json");

handler.addFilter(holder, "/*", EnumSet.of(DispatcherType.REQUEST));

handler.addEventListener(new GuiceServletConfig());

server.setHandler(handler);

server.start();
server.join();

While i do request in debug i "doFilter" method not called

Was it helpful?

Solution

add GzipFilter first, before GuiceFitler and CORSFilter

OTHER TIPS

But when using a ServletModule installed on your injector you can just add it in your module configureServlets() function like this.

    final Map<String, String> params = Maps.newHashMap();
    params.put("mimeTypes", "text/html,text/xml,text/plain,application/javascript,application/json");
    params.put("methods", "GET,PUT,POST,DELETE");
    params.put("deflateCompressionLevel", "9");
    bind(GzipFilter.class).in(Singleton.class);
    filter("/*").through(GzipFilter.class, params);

More info see Guice ServletModule#dispatch-order

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top