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

╄→尐↘猪︶ㄣ 提交于 2019-12-01 06:22:15

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:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!