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
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.
Just register it normally, where the result is the filter always getting called. (what you don't want).
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 {}
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: