非servlet容器中的Jersey过滤器 [英] Jersey filters in non-servlet container

查看:126
本文介绍了非servlet容器中的Jersey过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在非servlet容器(Netty)中运行Jersey.对于基于Servlet的容器​​,我可以使用:

I am running Jersey in a non-servlet container (Netty). For servlet-based containers, I can plug in a request filter using :

<init-param>
         <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
         <param-value>com.sun.jersey.api.container.filter.GZIPContentEncodingFilter</param-value>
</init-param>

但是我该如何以编程方式执行此操作?

But how do I do this programmatically in my case ?

推荐答案

这里是一个完全非servlet的示例: 假设已创建请求和/或响应过滤器,则可以将它们添加到启动代码中,如下所示: (请注意,在此示例中,ApiInterceptor类既是请求过滤器,也是响应过滤器)

Here is a completely non-servlet example: Assuming you have created your request and/or response filter(s), you can add them to your startup code as follows: (Please note ApiInterceptor class is both a request and a response filter in this example)

    final URI BASE_URI = UriBuilder.fromUri("http://localhost/").port(9999).build();

    System.out.println("Investigating Api services...");        
    ResourceConfig rc = new PackagesResourceConfig(
        "path.to.your.resource.objects");

    System.out.println("Registering interceptors...");
    rc.getProperties().put(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, ApiInterceptor.class.getName());
    rc.getProperties().put(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS, ApiInterceptor.class.getName());

    Debug.print("Starting grizzly...");
    GrizzlyServerFactory.createHttpServer(BASE_URI, rc);

    Debug.print("The app started @", BASE_URI.toString());
    Debug.print("Enjoy!");

    System.in.read();

这篇关于非servlet容器中的Jersey过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆