Is there an Interceptor in Jersey similar to that of Spring's HandlerInterceptor

后端 未结 1 955
盖世英雄少女心
盖世英雄少女心 2021-01-13 05:59

I need an Interceptor in Jersey 2.x which gives references to request, response and the Method which matched the Path to a Web Service.

Something similar to

相关标签:
1条回答
  • 2021-01-13 06:38

    Is there anyother way this can be achieved without using Filters. because I need this processing to happen ONLY if the corresponding web-service is present. Filter's with /* on the other hand would always perform these validations even when the resource was not found.

    There are different ways to register a filter.

    1. Just register it normally, where the result is the filter always getting called. (what you don't want).

    2. Registered with an annotation, though name binding. This way, only resource annotated will go through the filter. (this is kind of what you want, only problem is you would need to annotate every class)

      @Target({TYPE, METHOD})
      @Retention(RetentionPolicy.RUNTIME);
      class @interface Filtered {}
      
      @Path("..")
      @Filtered
      public class YourResource {}
      
      @Filtered
      @Provider
      public class YourFilter implements ContainerRequestFilter {}
      
    3. Use a DynamicFeature to bind resource programmatically, rather than declaratively. The DynamicFeture will get called for each of your resource methods, so you can just register the filter for every call. This has the same affect as annotating every resource class (as mentioned above) with name binding (this is probably what you want).

      @Provider
      public class MyFeature implements DynamicFeature {
          @Override
          public void configure(ResourceInfo ri, FeatureContext ctx) {
              ctx.register(YourFilter.class);
          }
      }
      

    See Also:

    • Chapter 10. Filters and Interceptors
    0 讨论(0)
提交回复
热议问题