2011-05-16 37 views
0

我在非servlet容器(Netty)中运行Jersey。对于基于servlet的容器​​,我可以用一个请求滤棒:非servlet容器中的泽西过滤器

<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> 

但我怎么在我的情况下做到这一点编程?

回答

0

不确定的Netty,但对于灰熊的:

..... 

    webServer = new GrizzlyWebServer(getPort(8080), ".", true); 

    // add Jersey resource servlet 

    ServletAdapter jerseyAdapter = new ServletAdapter(); 
    jerseyAdapter.addInitParameter("com.sun.jersey.config.property.packages", "com.your.resources.package"); 
    jerseyAdapter.setContextPath("/"); 
    jerseyAdapter.setServletInstance(new ServletContainer()); 

    // add the Container filter 
    jerseyAdapter.addInitParameter(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, GZIPContentEncodingFilter.class.getName()); 


    webServer.addGrizzlyAdapter(jerseyAdapter, new String[]{"/"}); 


    try { 
     // start Grizzly embedded server // 
     System.out.println(String.format("Jersey app started with WADL at %sapplication.wadl", BASE_URI)); 
     webServer.start(); 
    } catch (Exception ex) { 
     System.out.println(ex.getMessage()); 
    } 
1

这里是一个完全非servlet的例子: 假设你已经创建了您的请求和/或响应滤波器(S),你可以将它们添加你的启动代码如下: (请注意ApiInterceptor类既是一个请求,在这个例子中的响应过滤器)

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();