Jersey 2.3 Setting Priority for ContainerRequestFilter
You can configure priorities for your filters. A couple options
Option 1:
Use the @Priority
for your filter classes, passing in a value (e.g. @Priority(1)
). The lower the number, the higher the priority (Don't need for anything special in web.xml or Application subclass)
@Priority(6000)
public class MyFilter1 ... {}
@Priority(6001)
public class MyFilter2 ... {}
See Priorities
Option 2:
Configure it programmatically into the application via an inject-able Configurable
. Something like
@ApplicationPath("/")
public class MyApplication extends Application {
public MyApplication(@Context Configurable configurable) {
configurable.register(MyFilter1.class, 1000);
configurable.register(MyFilter2.class, 1001);
}
}
Or with ResourceConfig
simply calling register
without the injected Configurable
. See the API for overloaded register
public ResourceConfig register(Object component, int bindingPriority)
e.g.
public class MyApplication extends ResourceConfig {
public MyApplication() {
...
register(TestFilter.class, 6000);
register(TestFilter2.class, 6001);*/
}
}
Note:
Just an FYI, there are built in constants from the Priorites
class.
public final class Priorities {
private Priorities() { }
public static final int AUTHENTICATION = 1000;
public static final int AUTHORIZATION = 2000;
public static final int HEADER_DECORATOR = 3000;
public static final int ENTITY_CODER = 4000;
public static final int USER = 5000;
}
These are used by some of the built in components of the framework, so you might want to consider these when giving a number to your priority. You could use these like
@Priority(Priorities.USER)
// or @Priority(Priorities.USER + 1)
public class MyFilter ... {}
curiousengineer
Updated on June 03, 2022Comments
-
curiousengineer almost 2 years
public class MyApplication extends ResourceConfig { public MyApplication() { /* // Register resources and providers using package-scanning. packages("com.keylesson.service"); // Register my custom provider - not needed if it's in my.package. register(TestFilter.class); register(TestFilter2.class);*/ } }
Above code after un-commenting is able to execute both filter classes but the sequenceing is TestFilter2 and then TestFilter. This Resourceconfig method is something I do not like much and I want to use old style web.xml for Jersey2.3. The jersey 1.x init-params for filters are not working in jersey2.3. Can anyone help me with sample web.xml for jersey2.3 that guarantees the execution of filters? and also in the sequence testfilter and then testfilter2?
-
curiousengineer over 9 yearsThanks, it works. This is very helpful. However, I am not very clear on the "Application" annotation. I read the official doc also, but it confuses me. And the code you suggested works regardless of @Application annotation. Can you elaborate a bit on that as well?
-
Paul Samsotha over 9 years
@ApplicationPath
will auto configure the class as a JAX-RS application, so you don't need to configure it in XML. TheApplication
class is the main JAX-RS application class.ResourceConfig
extends from it. You can either subclassApplication
directly or with Jersey only, subclassResourceConfig
.ResourceConfig
adds some helper methods to make configuration easier, but it is not portable, as only Jersey uses this class. SubclassingApplication
is portable. See Application Deployment -
Puce over 7 yearsInjecting a Configurable to the constructor results in a NullPointerException. How can I obtain the Configurable?
-
Puce over 7 yearsI solved it by implementing and registring a custom Feature which registers the LoggingFilter.
-
Waleed almost 5 yearsThe answer is pretty well written however I'd like to point out that the register method in ResourceConfig doesn't work. This is probably true for all programatic ways of defining filter priorities in jersey. See following links: github.com/eclipse-ee4j/jersey/issues/3467 stackoverflow.com/questions/35914944/… stackoverflow.com/questions/34639434/…
-
Leandro over 4 yearsDoes a subclass have the same priority as its parent?
-
Paul Samsotha over 4 years@Leandro that's a good question. It's not something I have ever tested.