为什么Jersey不遵循动态绑定过滤器中的优先级? [英] Why Jersey doesn't honor the priorities in dynamically bind filters?
问题描述
我在泽西岛DynamicFeature >将容器请求过滤器动态绑定到某些特定的资源方法.除了DynamicFeature
,我还具有适用于所有资源方法的常规过滤器.但是,我发现这些过滤器的优先级/顺序无法按预期工作.例如:
I am using the DynamicFeature
in Jersey to dynamically bind a container request filter to some particular resource methods. In addition to the DynamicFeature
, I also have regular filters that apply to all resource methods. However, I found the priorities/orders of these filters not working as expected. For example:
public class MyDynamicFeature implements DynamicFeature {
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext featureContext) {
featureContext.register(MyDynamicFilter.class, 2);
}
}
public class MyDynamicFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
System.out.println("Hey! I am dynamic!");
}
}
public class MyStaticFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
System.out.println("Hey! I am static!");
}
}
在ResourceConfig
中,我注册DynamicFeature
并使用优先级进行过滤:
In the ResourceConfig
, I register the DynamicFeature
and filters with priorities:
register(MyDynamicFeature.class, 1);
register(MyStaticFilter.class, 3);
理想情况下,如果请求通过过滤器,我希望可以从日志中看到:
Ideally, if requests comes through the filters, I'd expect to see from the log:
Hey! I am dynamic!
Hey! I am static!
因为动态过滤器的优先级高于静态过滤器,但是我看到了
because the dynamic filter has a higher priority than the static one, but instead, I saw
Hey! I am static!
Hey! I am dynamic!
因此,似乎动态绑定的过滤器总是排在静态绑定的过滤器之后.为什么它不兑现我设定的优先事项?
So it seems like dynamically bound filters always come at the end after the statically bound ones. Why doesn't it honor the priorities I set?
推荐答案
尝试使用javax.annotation.Priority
注释,而不是FeatureContext::register
和ResourceConfig::register
调用中的第二个参数,该参数会覆盖注释.
Try to use javax.annotation.Priority
annotations instead of the 2nd parameter in FeatureContext::register
and ResourceConfig::register
calls, which overrides the annotation.
@Priority(1)
public class MyDynamicFilter implements ContainerRequestFilter {
...
@Priority(2)
public class MyStaticFilter implements ContainerRequestFilter {
...
featureContext.register(MyDynamicFilter.class);
...
register(MyDynamicFeature.class);
register(MyStaticFilter.class);
如果导致相同的问题,请更新您的依赖项.
And if that leads to the same problem, update your dependencies.
这篇关于为什么Jersey不遵循动态绑定过滤器中的优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!