在JAX-RS中使用@ Context,@ Provider和ContextResolver [英] Using @Context, @Provider and ContextResolver in JAX-RS
问题描述
我只是熟悉使用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屋!