JAX-RS DynamicFilter not being called for sub-reso

2019-06-26 05:50发布

According to the documentation it should be possible to use DynamicFeature for resources and sub-resources. As an effect I expect to have filter registered and called whenever sub-resource endpoint is being called - ie GET /some/sub/x. However following is not working for me (nor other similar approaches):

  • annotation

    @NameBinding
    @Target({METHOD, TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface SomeFilterAnn {}
    
  • resource

    @SomeFilterAnn
    @Path("/some")
    public class SomeResource {
      @GET
      public String getSome() {
        return "some";
      }
    
      @Path("/sub")
      public SubResource getSub() {
        return new SubResource();
      }
    
      @SomeFilterAnn
      @Path("/x")
      public static class SubResource {
        @GET
        public String getSome() {
          return "sub";
        }
      }
    }
    
  • filter (it is actually more complex and configured by DynamicFilter)

    @SomeFilterAnn
    public class SomeFilter implements ContainerRequestFilter {
      private static final Logger LOG = LoggerFactory.getLogger(SomeFilter.class);
    
      @Override
      public void filter(ContainerRequestContext requestContext) throws IOException {
        LOG.info("filter called");
      }
    }
    
  • here is DynamicFeature that I'd like to register filter for resource and sub-resource. Actually I would expect that configure() would be called also for SubResource so resourceInfo.getResourceClass() == SubResource.class but cannot manage to achieve this.

    @Provider
    public class SomeDynamicFeature implements DynamicFeature {
      private static final Logger LOG = LoggerFactory.getLogger(SomeDynamicFeature.class);
    
      @Override
      public void configure(ResourceInfo resourceInfo, FeatureContext context) {
        Class<?> resourceClass = resourceInfo.getResourceClass();
    
        // this works
        if (resourceClass == SomeResource.class) {
          LOG.info("register resource filter");
          context.register(new SomeFilter(/*some config*/));
        }
    
        // this does not work (not called for Subresource)
        if (resourceClass == SomeResource.SubResource.class) {
          LOG.info("register resource filter");
          context.register(new SomeFilter(/*some config*/));
        }
    
        // this also does not work (filter not called for Subresource)
        if (resourceInfo.getResourceMethod().getReturnType() == SomeResource.SubResource.class) {
          LOG.info("register subresource filter");
          context.register(new SomeFilter(/*some config*/));
        }
      }
    }
    

I'm running this on WildFly 8.2 with CXF and without Jersey or RESTEasy

EDIT

I have found that annotating SomeFilter with @Provider allows filter to works without need of DynamicFeature however I don't know then how to configure my filter - here is separate question about it.

0条回答
登录 后发表回答