hacker / welder / mechanic / carpenter / photographer / musician / writer / teacher / student
Musings of an Earth-bound carbon-based life form.
If you want to write a JAX-RS filter that only applies to methods that
are given a specific annotation, you can use the @NameBinding
annotation
for static binding of filters to methods. For example you could create a FilteringFilter
that you only want to apply to methods annotated with @Filtered
.
Note: if you separate your implementation from your interface then you will need to apply the annotation to the interface, not the implementation.
To do this you would want to create the annotation and the filter, as follows:
@NameBinding
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Filtered { }
@Provider
@Filtered
public class FilteringFilter implements ContainerRequestFilter,
ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
System.out.println("BEFORE");
}
@Override
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
System.out.println("AFTER");
}
}
class ServiceApi {
@GET
@Logged
String getName(@PathParam("userId") String userId);
}