Jersey 2.4 ReaderInterceptor is not working

httprequest, java, jersey-2.0

Solution

Interceptors (implementations of ReaderInterceptor / WriterInterceptor) are executed only if request/response entity is available. In your case this means that only `WriterInterceptor` is being executed since you're sending entity (an instance of `FooObj`) to the client from your resource method. If you had a `POST` method that receives an input from user your `ReaderInterceptor` would be invoked as well.

In case you need to modify the request even if no entity is present use ContainerRequestFilter / ContainerResponseFilter.

See JAX-RS 2.0 spec for more info.

Problem

I'm testing Jersey interceptors and filters. I have this Jersey 2.4 interceptor code: ``` @Provider @Test public class TestInterceptor implements WriterInterceptor, ReaderInterceptor { private final static Logger log = .... @Override public void aroundWriteTo (WriterInterceptorContext context) throws IOException, WebApplicationException { log.debug("WriterInterceptor"); context.proceed(); } @Override public Object aroundReadFrom(ReaderInterceptorContext ric) throws IOException, WebApplicationException { log.debug("ReaderInterceptor"); return ric.proceed(); } } ``` my resource method: ``` @Path("{test}") @GET @Produces(MediaType.APPLICATION_JSON) @Test public FooObj test () { log.debug("test method"); return new FooObj(); } ``` two filters: ``` @Provider public class ResponseFil implements ContainerResponseFilter { private final static Logger log = .... @Override public void filter(ContainerRequestContext crc, ContainerResponseContext crc1) throws IOException { log.debug("ResponseFil"); } } @Provider public class RequestFil implements ContainerRequestFilter { private final static Logger log = .... @Override public void filter(ContainerRequestContext crc) throws IOException { log.debug("RequestFil"); } } ``` and my web.xml servlet configuration: ``` <servlet> <description>Servlet test</description> <servlet-name>REST_servlet</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.test.rest</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> ``` When from firefox I type the url address resource and get the result, log console shows: ``` RequestFil test method ResponseFil WriterInterceptor ``` Why ReaderInterceptor isn't executed? I have tried to separate write and reader interceptors in two classes with two custom binding names (@Test and @Test2) with the same result. Thanks

Original source