如何在 Neo4j 非托管扩展中进行压缩响应?

How to make a gzipped response in a Neo4j unmanaged extension?

嗨,

我正在尝试在 Neo4j 非托管扩展中进行 gzipped 响应。我找到了这个例子:http://www.codingpedia.org/ama/how-to-compress-responses-in-java-rest-api-with-gzip-and-jersey/

我尝试添加这个 WriterInterceptor:

@Provider
@Compress
public class GZIPWriterInterceptor implements WriterInterceptor {

    @Override
    public void aroundWriteTo(WriterInterceptorContext context)
            throws IOException, WebApplicationException {

        MultivaluedMap<String,Object> headers = context.getHeaders();
        headers.add("Content-Encoding", "gzip");

        final OutputStream outputStream = context.getOutputStream();
        context.setOutputStream(new GZIPOutputStream(outputStream));
        context.proceed();
    }
}

还有这个注释:

@NameBinding
@Retention(RetentionPolicy.RUNTIME)
public @interface Compress {
}

在我的资源中,我是这样使用它的:

@GET
@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
@Compress
public List<User> getUsers() {
    return factory.getUserService(graphDb).getUsers();
}

但是从未调用 WriterInterceptor,为什么?我怎样才能使我的回复压缩?

我想在我的非托管插件和解决方案中解决这个问题,其中响应在 Neo4j 之外的代理中被 gzip 压缩是不可替代的。

您需要注册一个关注 gzip 压缩的 servlet 过滤器。这可以在 SPIPluginLifecycle 中完成,请参阅此博客 post:http://www.markhneedham.com/blog/2013/07/08/neo4j-unmanaged-extension-creating-gzipped-streamed-responses-with-jetty/