没有为子资源调用JAX-RS DynamicFilter

时间:2015-10-24 21:50:59

标签: java rest jax-rs cxf

根据文档,应该可以使用DynamicFeature来获取资源和子资源。作为一种效果,我期望在调用子资源端点时注册并调用过滤器 - 即GET /some/sub/x。然而,以下对我不起作用(也没有其他类似方法):

 
  • 注释

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

    @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";
        }
      }
    }
    
  • 过滤器(它实际上更复杂,由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");
      }
    }
    
  • 这里是DynamicFeature,我想为资源和子资源注册过滤器。实际上我希望configure()也会SubResource调用resourceInfo.getResourceClass() == SubResource.class所以@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*/)); } } } ,但无法实现此目标。

    SomeFilter

我在使用CXF且没有Jersey或RESTEasy的WildFly 8.2上运行此操作

修改

我发现使用@Provider注释DynamicFeature可以让过滤器无需sudo echo 0 > /proc/sys/kernel/randomize_va_space 即可运行,但我不知道如何配置我的过滤器 - 这里是{{ 3}}关于它。

0 个答案:

没有答案