Using @Context, @Provider and ContextResolver in JAX-RS

I don’t think there’s a JAX-RS specific way to do what you want. The closest would be to do:

class MyResource {
    @Context providers;

    public Response get() {
        ContextResolver<StorageEngine> resolver = providers.getContextResolver(StorageEngine.class, MediaType.WILDCARD_TYPE);
        StorageEngine engine = resolver.get(StorageEngine.class);

However, I think the annotation and is really for types related to JAX-RS and supporting JAX-RS providers.

You may want to look for Java Context and Dependency Injection (JSR-299) implementations (which should be available in Java EE 6) or other dependency injection frameworks such as Google Guice to help you here.

Leave a Comment