Jersey 2.3 Priority Determination for ContainerRequestFilter

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 in my.package. register(TestFilter.class); register(TestFilter2.class);*/ } } 

You can execute both filter classes on the code after un-commenting, but the sequence is TestFilter2 and then TestFilter. This Resourceconfig method is something that I don't like, and I want to use the old web.xml style for Jersey2.3. 1.x trigger parameters for filters do not work in jersey2.3. Can someone help me with a web.xml example for jersey2.3 that guarantees filter execution? as well as in the test sequence filter and then testfilter2?

+7
java jersey jax-rs
source share
1 answer

You can prioritize your filters. A couple of options

Option 1:

Use @Priority for your filter classes, passing in a value (e.g. @Priority(1) ). The lower the number, the higher the priority (nothing special is needed in web.xml or a subclass of the application)

 @Priority(6000) public class MyFilter1 ... {} @Priority(6001) public class MyFilter2 ... {} 

See Priorities

Option 2:

Configure it programmatically in an application using Injectable 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 using ResourceConfig just calling register without the Configurable entered. See API for register overload

 public ResourceConfig register(Object component, int bindingPriority) 

eg.

 public class MyApplication extends ResourceConfig { public MyApplication() { ... register(TestFilter.class, 6000); register(TestFilter2.class, 6001);*/ } } 

Note:

Just FYI, constants from the Priorites class are Priorites .

 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; } 

They are used by some of the built-in components of the framework, so you can consider them by indicating the number at your priority. You can use them as

 @Priority(Priorities.USER) // or @Priority(Priorities.USER + 1) public class MyFilter ... {} 
+11
source share

All Articles