简体   繁体   中英

How to set priorities and annotations on Jersey filters

Imagine two filters and there annotations as follow:

@Provider
@ToTokenFilter
@Priority(1000)
public class TokenFilter implements ContainerRequestFilter {
  @Override
  public void filter() {
    // to do
  }
}

@Provider
@ToRootFilter
@Priority(2000)
public class RootFilter implements ContainerRequestFilter {
  @Override
  public void filter() {
    // to do
  }
}

@NameBinding
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface ToTokenFilter {}

@NameBinding
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface ToRootFilter {}

I'm trying to use this filters on a class, something like :

@Path("/users")
@ToTokenFilter
@ToRootFilter
public class Users {
  @GET
  public String getUsers() {
    //to do
  }
}

This way, my code is never going in my TokenFilter, and if I remove the @ToRootFilter, it's finally going. But I need both ! And I need TokenFilter to be call before RootFilter. That's why I did set priorities, I tried to put values priorities in both way but nothing is working.

I tried to put @ToTokenFilter on the class and @ToRootFilter on the method, but still the same issue.

Does someone knows how to use multiple filters with priorities and annotations with Jersey?

EDIT

here is the web.xml

<init-param>
    <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
    <param-value>filters.TokenFilter;filters.RootFilter</param-value>
</init-param>

I finally found the problem :

i was importing :

import com.sun.jersey.core.util.Priority;

instead of

import javax.annotation.Priority;

But jersey is using java Priority annotation to set Priority on filter.

By default, Priority was set to 5000 on all my filters, and the sequence was random i guess, sometimes using TokenFilter first, and sometimes RootFilter. And my RootFilter function was stopping the request because it needed an attribute set in TokenFilter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM