users@jersey.java.net

[Jersey] Re: Problem with Request/response filters

From: Jose Antonio Illescas Del Olmo <jantonio.illescas_at_rbcdexia-is.es>
Date: Tue, 16 Aug 2011 17:01:37 +0200

Fixed,

If you use Guice you must remove Jersey references:

    remove servlet
com.sun.jersey.spi.container.servlet.ServletContainer) from web.xml

And configure Guice programatically (on custom ServletcontextListener):

   web.xml___________________________

<filter>
<filter-name>GuiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>GuiceFilter</filter-name>
<url-pattern>/rest/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>


<listener>
<listener-class>mypackage.ContextListener</listener-class>
</listener>


   Custom ServletContextListener_________

    public class ContextListener GuiceServletContextListener {

         @Override
         protected Injector getInjector() {
             return Guice.createInjector(new JerseyServletModule() {

                 @Override
                 protected void configureServlets() {
                     Map<String, String> params = new HashMap<String,
    String>();
                    
    params.put(PackagesResourceConfig.PROPERTY_PACKAGES, "mypackage");
                    
    params.put(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS,
    "com.sun.jersey.api.container.filter.LoggingFilter");
                    
    params.put(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS,"com.sun.jersey.api.container.filter.LoggingFilter");

                     bind(...)
                     serve("/rest/*").with(GuiceContainer.class, params);
                 }
             });
         }

Thanks


On 12/08/2011 16:39, Jose Antonio Illescas Del Olmo wrote:
> I see that ContainerResponseFilters must by one unique init-param join
> class names with ";"
>
> after join my classes same error: filters don't intercepts nothing
>
> On 12/08/2011 12:16, Jose Antonio Illescas Del Olmo wrote:
>> I trying custom handlers with filters but not works: never execute
>> filter code, I checking with:
>>
>> · custom response filter: OutputResponseFilter
>> · logging request/response filter: LoggingFilter
>>
>> This is my web.xml configuration (and I use Guice):
>>
>> <filter>
>> <filter-name>GuiceFilter</filter-name>
>> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
>> </filter>
>>
>> <filter-mapping>
>> <filter-name>GuiceFilter</filter-name>
>> <servlet-name>JerseyServlet</servlet-name>
>> <dispatcher>REQUEST</dispatcher>
>> <dispatcher>ERROR</dispatcher>
>> <dispatcher>INCLUDE</dispatcher>
>> <dispatcher>FORWARD</dispatcher>
>> </filter-mapping>
>>
>> <servlet>
>> <servlet-name>JerseyServlet</servlet-name>
>> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
>>
>> <init-param>
>> <param-name>com.sun.jersey.config.property.packages</param-name>
>> <param-value>mypackage</param-value> <!-- ';' separated packages -->
>> </init-param>
>> <init-param>
>> <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
>>
>> <param-value>mypackage.OutputResponseFilter</param-value>
>> </init-param>
>> <init-param>
>> <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
>>
>> <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
>>
>> </init-param>
>> <init-param>
>> <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
>>
>> <param-value>com.sun.jersey.api.container.filter.LoggingFilter</param-value>
>>
>> </init-param>
> Now, I have single
> "com.sun.jersey.spi.container.ContainerResponseFilters" as:
>
> <init-param>
> <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
>
> <param-value>com.sun.jersey.api.container.filter.*LoggingFilter*;mypackage.*OutputResponseFilter*</param-value>
>
> </init-param>
>
>
>> <load-on-startup>1</load-on-startup>
>> </servlet>
>>
>> <servlet-mapping>
>> <servlet-name>JerseyServlet</servlet-name>
>> <url-pattern>/rest/*</url-pattern>
>> </servlet-mapping>
>>
>>
>