在JAX-RS中使用@ Context,@ Provider和ContextResolver [英] Using @Context, @Provider and ContextResolver in JAX-RS

查看:288
本文介绍了在JAX-RS中使用@ Context,@ Provider和ContextResolver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是熟悉使用JAX-RS在Java中实现REST Web服务,因此遇到了以下问题.我的资源类之一要求访问存储后端,该后端在StorageEngine接口后面抽象.我想将当前StorageEngine实例注入到服务REST请求的资源类中,我认为这样做的一种好方法是使用@Context批注和适当的ContextResolver类.这是我到目前为止的内容:

I'm just getting acquainted with implementing REST web services in Java using JAX-RS and I ran into the following problem. One of my resource classes requires access to a storage backend, which is abstracted away behind a StorageEngine interface. I would like to inject the current StorageEngine instance into the resource class serving the REST requests and I thought a nice way of doing this would be by using the @Context annotation and an appropriate ContextResolver class. This is what I have so far:

MyResource.java中:

class MyResource {
    @Context StorageEngine storage;
    [...]
}

StorageEngineProvider.java中:

@Provider
class StorageEngineProvider implements ContextResolver<StorageEngine> {
    private StorageEngine storage = new InMemoryStorageEngine();

    public StorageEngine getContext(Class<?> type) {
        if (type.equals(StorageEngine.class))
            return storage;
        return null;
    }
}

我正在使用com.sun.jersey.api.core.PackagesResourceConfig自动发现提供程序和资源类,并且根据日志,它很好地拾取了StorageEngineProvider类(故意遗漏了时间戳和不必要的内容):

I'm using com.sun.jersey.api.core.PackagesResourceConfig to discover the providers and the resource classes automatically, and according to the logs, it picks up the StorageEngineProvider class nicely (timestamps and unnecessary stuff left out intentionally):

INFO: Root resource classes found:
    class MyResource
INFO: Provider classes found:
    class StorageEngineProvider

但是,资源类中storage的值始终为null-泽西岛从未调用过StorageEngineProvider的构造函数或其getContext方法.我在这里做什么错了?

However, the value of storage in my resource class is always null - neither the constructor of StorageEngineProvider nor its getContext method is called by Jersey, ever. What am I doing wrong here?

推荐答案

我认为没有JAX-RS特定的方法可以执行所需的操作.最接近的是:

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

@Path("/something/")
class MyResource {
    @Context
    javax.ws.rs.ext.Providers providers;

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

但是,我认为@ javax.ws.rs.core.Context批注和javax.ws.rs.ext.ContextResolver确实适用于与JAX-RS相关并支持JAX-RS提供程序的类型.

However, I think the @javax.ws.rs.core.Context annotation and javax.ws.rs.ext.ContextResolver is really for types related to JAX-RS and supporting JAX-RS providers.

您可能需要寻找Java上下文和依赖注入(JSR-299)实现(应在Java EE 6中使用)或其他依赖注入框架(例如Google Guice)来为您提供帮助.

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.

这篇关于在JAX-RS中使用@ Context,@ Provider和ContextResolver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆