Unable to get @NameBinding working for jaxrs Interceptors with cxf

176 Views Asked by At

I have 2 interceptors for request-response logging purposes. I am trying to get my Reader/Writer Interceptor to work with @NameBinding. It works if I add the providers in beans.xml for the server as below :

<jaxrs:providers>
   <ref bean="jsonProvider"/>
   <ref bean="jaxbProvider" />
   <ref bean="fooReaderInterceptor"/>
   <ref bean="fooWriterInterceptor"/>
<jaxrs:providers>

But as this binds the interceptors with all the resources, I tried the below to use @NameBinding and bind it only to specific resources.

FooAnnotation.java :

package a;

import javax.ws.rs.NameBinding;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@NameBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface fooAnnotation {
}

FooReaderInterceptor/FooWriterInterceptor :

package a;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.ReaderInterceptor;
import javax.ws.rs.ext.ReaderInterceptorContext;



@Provider
@FooAnnotation
public class FooReaderInterceptor implements ReaderInterceptor {

   //some logging logic
}

ResourceInterface:

package b;

@Path("somePath")
public interface SomeService {

    @POST
    @Consumes({"application/xml", "application/json"})
    @Produces({"application/xml", "application/json"})
    @Path("/path")
    @FooAnnotation
    Response someResource();

//other resources

}

ResourceImpl :


package b
...

@Override
@FooAnnotation
public Response someResource(){
       //business logic
}

Dependencies used :

cxf-minimal-bundle : 2.7.16
cxf-rt-frontend-jaxrs : 3.1.6

Using the above code, the Interceptors doesn't get called when I fire the annotated resource. Need help! Please let me know if any more info is required.

0

There are 0 best solutions below