我在注入ResourceInfo到球衣過濾器時遇到問題。當擁有一個Spring託管過濾器實例時,註解@Context似乎無法正常工作。ResourceInfo沒有注入到Spring管理球衣過濾器
我發現這是什麼: Jersey Request Filter only on certain URI
第二個目標是相當類似我的情況。
DynamicFeature:
@Provider
public class HttpCacheConfigDynamicFeature implements DynamicFeature {
private final DefaultHttpCacheFilter defaultHttpCacheFilter;
private final HttpCacheFilter httpCacheFilter;
@Autowired
public HttpCacheConfigDynamicFeature(DefaultHttpCacheFilter defaultHttpCacheFilter, HttpCacheFilter httpCacheFilter) {
this.defaultHttpCacheFilter = defaultHttpCacheFilter;
this.httpCacheFilter = httpCacheFilter;
}
...
篩選:
@Component
public class HttpCacheFilter implements ContainerResponseFilter {
private Log logger = LogFactory.getLog(HttpCacheFilter.class);
private final ConfigurationController configurationController;
@Context
private ResourceInfo resourceInfo;
@Autowired
public HttpCacheFilter(ConfigurationController configurationController) {
this.configurationController = configurationController;
}
...
資源信息總是空。
我現在做的是我的dynamicFeature改變一點是:
@Provider
public class HttpCacheConfigDynamicFeature implements DynamicFeature {
private final DefaultHttpCacheFilter defaultHttpCacheFilter;
private final ConfigurationController configurationController;
@Autowired
public HttpCacheConfigDynamicFeature(DefaultHttpCacheFilter defaultHttpCacheFilter, ConfigurationController configurationController) {
this.defaultHttpCacheFilter = defaultHttpCacheFilter;
this.configurationController = configurationController;
}
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext featureContext) {
if (hasHttpCacheConfigAnnotation(resourceInfo)) {
featureContext.register(new HttpCacheFilter(configurationController, resourceInfo), 50);
} else {
featureContext.register(defaultHttpCacheFilter, 51);
}
}
也許你知道一個更優雅的方式來實現自己的目標。
謝謝!
克里斯托夫