简体   繁体   English

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

[英]How to make a gzipped response in a Neo4j unmanaged extension?

Hi, 嗨,

I am trying to make a gzipped response in a Neo4j unmanaged extension. 我试图在Neo4j非托管扩展中进行gzipped响应。 I found this example: http://www.codingpedia.org/ama/how-to-compress-responses-in-java-rest-api-with-gzip-and-jersey/ 我找到了这个例子: http//www.codingpedia.org/ama/how-to-compress-responses-in-java-rest-api-with-gzip-and-jersey/

I tried to add this WriterInterceptor: 我试图添加这个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();
    }
}

And this annotation: 而这个注释:

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

And in my resource I use it like this: 在我的资源中,我使用它像这样:

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

However the WriterInterceptor is never called, why? 但是从不调用WriterInterceptor,为什么? How can I make my response gzipped? 我怎样才能让我的回复被压缩?

I want to solve this inside my unmanaged plugin and solutions where the response is gzipped in a proxy outside of Neo4j is not an alternative. 我想在我的非托管插件和解决方案中解决这个问题,其中响应是在Neo4j之外的代理中进行压缩而不是替代方案。

You need to register a servlet filter that cares about gzipping. 您需要注册一个关心gzipping的servlet过滤器。 This can be done in a SPIPluginLifecycle , see this blog post: http://www.markhneedham.com/blog/2013/07/08/neo4j-unmanaged-extension-creating-gzipped-streamed-responses-with-jetty/ 这可以在SPIPluginLifecycle完成,请参阅此博客文章: http//www.markhneedham.com/blog/2013/07/08/neo4j-unmanaged-extension-creating-gzipped-streamed-responses-with-jetty/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM