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?
You can configure priorities for your filters. A couple options
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
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);*/
}
}
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 ... {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With