在Jersey2中使用@Immediate注释 [英] @Immediate annotation use in Jersey2
本文介绍了在Jersey2中使用@Immediate注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到类似的问题:如何让我的Jersey 2端点在启动时急切地初始化?
但稍微下行了。我可以立即加载我的资源,但是当我尝试通过调用REST URL来使用它时,我得到以下堆栈跟踪。
But slightly further down the line. I can get my resource immediately loaded, but when I try to use it by calling the REST url, I get the following stack trace.
java.lang.IllegalStateException: Could not find an active context for org.glassfish.hk2.api.Immediate
2. java.lang.IllegalStateException: While attempting to create a service for
SystemDescriptor(
implementation=com.service.MyResource
contracts={com.service.MyResource}
scope=org.glassfish.hk2.api.Immediate
qualifiers={}
descriptorType=CLASS
descriptorVisibility=NORMAL
metadata=
rank=0
loader=null
proxiable=null
proxyForSameScope=null
analysisName=null
id=150
locatorId=0
identityHashCode=1249600275
reified=true) in scope org.glassfish.hk2.api.Immediate an error occured while locating the context
我的TResource类因此被注释:
My TResource class is annotated thus:
@Immediate
@Path("/test/v1")
public class TResource {
我的基于Grizzly的服务器设置如下:
My Grizzly based server is setup like so:
ResourceConfig rc = new ResourceConfig()
.packages(true,
"com.mystuff"
)
.property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true");
HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(URI.create(base_uri), rc);
ApplicationHandler handler = new ApplicationHandler(rc);
ServiceLocatorUtilities.enableImmediateScope(handler.getServiceLocator());
任何指导都将非常感谢!
欢呼,
菲尔
Any guidance would be most appreciated ! cheers, Phil
推荐答案
获取句柄的一种方法ServiceLocator
是实现 功能
。
One way to get a handle on the ServiceLocator
is to implement a Feature
.
import javax.inject.Inject;
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
public class ImmediateFeature implements Feature {
@Inject
public ImmediateFeature(ServiceLocator locator) {
ServiceLocatorUtilities.enableImmediateScope(locator);
}
@Override
public boolean configure(FeatureContext context) {
return true;
}
}
然后只需注册功能
ResourceConfig rc = new ResourceConfig().packages("jersey.hk2.test");
rc.register(ImmediateFeature.class);
我测试了这个并且工作正常
I've tested this and it works fine
这篇关于在Jersey2中使用@Immediate注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文