I am having problems injecting the ResourceInfo into my jersey filter. The annotation @Context doesn't seem to work properly when having a spring managed filter instance.
What I found is this: Jersey Request Filter only on certain URI
The second goal is quite similar to my situation.
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;
}
...
Filter:
@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;
}
...
The resource info is always null.
What I do now is to change my dynamicFeature a little to that:
@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);
}
}
Maybe you know a more elegant way to achieve my goals.
Thanks!
Christoph
In normal cases it would work, as Jersey would still inject it. But there is a known issue with the DynamicFeature and registering instances. If you register it as a class (ie register(HttpCacheFilter.class)
), then injections will work.
In your case, I don't see any reason you need to manually pass the ConfigurationController
to the HttpCacheFilter
that you instantiate. If you just register the class like in the previous example, it will automatically be injected.
Also (not tested) but I think you can even clean it up and just use @Inject
on the constructor of the filter. Then you can have both the ResourceInfo
and the ConfigurationController
both as constructor arguments. Not a big deal, but to me it's looks cleaner just having one injection point. Like I said, I didn't test it, but I'm pretty sure it will work. @Inject
and @Context
are pretty much interchangeable with Jersey, and you can also use @Inject
for Spring components in Jersey, so I would imagine that it just works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.