10

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.

Viccari
  • 9,029
  • 4
  • 43
  • 77

1 Answers1

8

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
RTeichmann
  • 96
  • 1
  • 3
  • Thanks for your suggestion. I tried it, but even after the change, the filter still ends up being called before every single call to my app, not only the ones annotated with @ValidateFoo. – Viccari Jun 26 '13 at 16:11
  • 4
    Ok, now we were able to make it work. It looks like RESTeasy did not like the `@PreMatching` annotation. By following your suggestion and removing the `@PreMatching` annotation, everything worked as expected. Thanks! – Viccari Jun 26 '13 at 17:20
  • 3
    It kind of makes sense, because `@PreMatching` means before mapping the request to RESTEasy resource, so at that point RESTEasy doesn't know if there is any namebinding. – stackoverflower May 21 '14 at 10:41
  • 1
    "Any `@NameBinding` named binding annotations will be ignored on a component annotated with the `@PreMatching` annotation." -- javax.ws.rs.container.PreMatching – relluf Sep 04 '14 at 19:13
  • 1
    just as an emphasis. Retention's default is CLASS and not runtime. thus you need it. – Eyad Ebrahim Jan 23 '15 at 13:53