Match Filter with specific Method through NameBind

2019-02-09 02:04发布

问题:

I am trying to specify a pre-matching filter that is only associated to some of my API calls, by following what the RESTeasy documentation suggests. Here is what my code looks like:

Name binding:

@NameBinding
public @interface ValidateFoo {}

Resource:

@Path("/foo/bar")
@Produces(MediaType.APPLICATION_JSON)
public class FooBar {
    @GET
    @ValidateFoo
    public Object doStuff() {
        //do stuff
    }

    @POST
    public Object doAnotherStuff() {
        //do another stuff
    }
}

Filter:

@ValidateFoo
@Provider
@PreMatching
public class FooValidation implements ContainerRequestFilter {
    @Override
    public void filter(ContainerRequestContext reqContext) throws IOException {
        //validate stuff
    }
}

Problem is: the FooValidation filter runs before every method call (e.g.: before GETs and POSTs to /foo/bar), not only the ones annotated with @ValidateFoo (seems a bug to me). If I remove the @Provider annotation from the filter, it won't run before any call (as expected).

I am seeing this behavior consistently, either using WebLogic or Tomcat. My dependency management is done through Maven, and the RESTeasy version is 3.0-beta-3.

Anyone experiencing/experienced the same behavior? I have seen another user with a similar problem on JBoss forums, with no luck so far.

UPDATE: Still experiencing the same issue with RESTeasy 3.0.1-Final.

回答1:

I had similar problem. For me the solution was to add following annotation configuration (to @ValidateFoo):

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(value = RetentionPolicy.RUNTIME)
@NameBinding