gpt4 book ai didi

java - Jersey2 中@Immediate 注解的使用

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:51 24 4
gpt4 key购买 nike

我有一个与此处提出的问题类似的问题:How do I get my Jersey 2 Endpoints to eagerly initialize on startup?

但稍微往下走。我可以立即加载我的资源,但是当我尝试通过调用 REST url 使用它时,我得到以下堆栈跟踪。

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 类是这样注释的:

@Immediate
@Path("/test/v1")
public class TResource {

我的基于 Grizzly 的服务器设置如下:

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());

任何指导将不胜感激!干杯,菲尔

最佳答案

获取 ServiceLocator 句柄的一种方法是实现 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);

我已经测试过了,它工作正常

关于java - Jersey2 中@Immediate 注解的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29289245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com