java 在未在活动 Jersey 错误范围内运行的代码中检测到 HK2 故障
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38530282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HK2 failure has been detected in a code that does not run in an active Jersey Error scope
提问by Sebastian Frey
Currently I am developing a jersey based RESTful application and would like to use DPI in my resources. (Note: Version of jersey is 2.23.1 and the servlet container is tomcat 8.5.3.)
目前我正在开发一个基于 jersey 的 RESTful 应用程序,并希望在我的资源中使用 DPI。(注:jersey 版本为 2.23.1,servlet 容器为 tomcat 8.5.3。)
Therefore I followed the tutorial Chapter 23. Custom Injection and Lifecycle Managementin the jersey docs and created a resource, a factory and bind the factory to a class like this:
因此,我遵循了jersey 文档中的教程第 23 章自定义注入和生命周期管理,并创建了一个资源、一个工厂并将工厂绑定到这样的类:
Resource:
资源:
@Path("/{project}/catalogs")
public class ProjectsResource {
@Inject
Project project;
...
}
Factory:
工厂:
public class ProjectFactory extends Factory<Project> {
private final Cache cache = cache.getInstance();
@PathParam("project")
private String project;
private HttpServletRequest request;
@Inject
public ProjectFactory(HttpServletRequest request) {
this.request = request;
}
@Override
public Project provide() {
return cache.get(project, Project.class);
}
@Override
public void dispose(Project instance) {}
}
Also I have a feature, which registers an AbstractBinder, which binds my ProjectFactory to my Project class.
我还有一个功能,它注册一个 AbstractBinder,它将我的 ProjectFactory 绑定到我的 Project 类。
@Provider
public class ProjectFeature implements Feature {
@Override
public boolean configure(FeatureContext context) {
context.register(new AbstractBinder() {
@Override
protected void configure() {
bindFactory(ProjectFactory.class)
.to(Project.class)
.proxy(false)
.proxyForSameScope(true)
.in(RequestScoped.class);
});
return true;
}
}
The actually problem is, when I call my Resource everything is fine and i can access my project instance, but in my tomcat catalina logs i am getting the following stacke trace:
实际问题是,当我调用我的资源时一切正常,我可以访问我的项目实例,但在我的 tomcat catalina 日志中,我得到以下堆栈跟踪:
22-Jul-2016 16:29:46.510 WARNING [pool-24-thread-1] org.glassfish.jersey.internal.Errors.logErrors The following warnings have been detected: WARNING: HK2 failure has been detected in a code that does not run in an active Jersey Error scope.
WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 3
java.lang.IllegalStateException: Not inside a request scope.
at jersey.repackaged.com.google.common.base.Preconditions.checkState(Preconditions.java:173)
at org.glassfish.jersey.process.internal.RequestScope.current(RequestScope.java:233)
at org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:158)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2022)
at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:765)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getUnqualifiedService(ServiceLocatorImpl.java:772)
at org.jvnet.hk2.internal.IterableProviderImpl.get(IterableProviderImpl.java:111)
at org.glassfish.jersey.server.internal.inject.AbstractContainerRequestValueFactory.getContainerRequest(AbstractContainerRequestValueFactory.java:71)
at org.glassfish.jersey.server.internal.inject.PathParamValueFactoryProvider$PathParamValueFactory.provide(PathParamValueFactoryProvider.java:93)
at org.glassfish.jersey.server.internal.inject.ParamInjectionResolver.resolve(ParamInjectionResolver.java:134)
at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:211)
at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:234)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:357)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:70)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2022)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:114)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:88)
at org.jvnet.hk2.internal.FactoryCreator.dispose(FactoryCreator.java:175)
at org.jvnet.hk2.internal.SystemDescriptor.dispose(SystemDescriptor.java:526)
at org.glassfish.jersey.process.internal.RequestScope$Instance.remove(RequestScope.java:532)
at org.glassfish.jersey.process.internal.RequestScope$Instance.release(RequestScope.java:549)
at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.resume(ServerRuntime.java:968)
at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.resume(ServerRuntime.java:922)
at de.moss.ems.rest.resource.AbstractBaseResource.send(AbstractBaseResource.java:118)
at de.moss.ems.rest.resource.AbstractBaseResource.resume(AbstractBaseResource.java:165)
at de.moss.ems.rest.resource.catalog.CatalogsResource.handleGet(CatalogsResource.java:49)
at de.moss.ems.rest.resource.catalog.AbstractCatalogResource.lambda$asyncGetRequestimport org.glassfish.hk2.api.PerLookup;
@Provider
public class ProjectFeature implements Feature {
@Override
public boolean configure(FeatureContext context) {
context.register(new AbstractBinder() {
@Override
protected void configure() {
bindFactory(ProjectFactory.class)
.to(Project.class)
.proxy(false)
.proxyForSameScope(true)
.in(PerLookup.class);
});
return true;
}
}
(AbstractCatalogResource.java:44)
at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1626)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
MultiException stack 2 of 3
java.lang.IllegalArgumentException: While attempting to resolve the dependencies of de.moss.ems.rest.factory.ProjectFactory errors were found
at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:246)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:357)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:70)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2022)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:114)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:88)
at org.jvnet.hk2.internal.FactoryCreator.dispose(FactoryCreator.java:175)
at org.jvnet.hk2.internal.SystemDescriptor.dispose(SystemDescriptor.java:526)
at org.glassfish.jersey.process.internal.RequestScope$Instance.remove(RequestScope.java:532)
at org.glassfish.jersey.process.internal.RequestScope$Instance.release(RequestScope.java:549)
at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.resume(ServerRuntime.java:968)
at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.resume(ServerRuntime.java:922)
at de.moss.ems.rest.resource.AbstractBaseResource.send(AbstractBaseResource.java:118)
at de.moss.ems.rest.resource.AbstractBaseResource.resume(AbstractBaseResource.java:165)
at de.moss.ems.rest.resource.catalog.CatalogsResource.handleGet(CatalogsResource.java:49)
at de.moss.ems.rest.resource.catalog.AbstractCatalogResource.lambda$asyncGetRequest@Inject
public void setContext(ContainerRequestContext context) {
this.context = context;
}
@Override
public SomeInfo provide() {
return (SomeInfo) context.getProperty(SOME_KEY);
}
(AbstractCatalogResource.java:44)
at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1626)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
MultiException stack 3 of 3
java.lang.IllegalStateException: Unable to perform operation: resolve on de.moss.ems.rest.factory.ProjectFactory
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:386)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
at org.jvnet.hk2.internal.PerLookupContext.findOrCreate(PerLookupContext.java:70)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2022)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:114)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:88)
at org.jvnet.hk2.internal.FactoryCreator.dispose(FactoryCreator.java:175)
at org.jvnet.hk2.internal.SystemDescriptor.dispose(SystemDescriptor.java:526)
at org.glassfish.jersey.process.internal.RequestScope$Instance.remove(RequestScope.java:532)
at org.glassfish.jersey.process.internal.RequestScope$Instance.release(RequestScope.java:549)
at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.resume(ServerRuntime.java:968)
at org.glassfish.jersey.server.ServerRuntime$AsyncResponder.resume(ServerRuntime.java:922)
at de.moss.ems.rest.resource.AbstractBaseResource.send(AbstractBaseResource.java:118)
at de.moss.ems.rest.resource.AbstractBaseResource.resume(AbstractBaseResource.java:165)
at de.moss.ems.rest.resource.catalog.CatalogsResource.handleGet(CatalogsResource.java:49)
at de.moss.ems.rest.resource.catalog.AbstractCatalogResource.lambda$asyncGetRequest@Inject
public void setContext(Factory<ContainerRequestContext> contextFactory) {
this.contextFactory = contextFactory;
}
@Override
public SomeInfo provide() {
ContainerRequestContext ctx = contextFactory.provide();
try {
return (SomeInfo) ctx.getProperty(SOME_KEY);
} finally {
contextFactory.dispose(ctx);
}
}
(AbstractCatalogResource.java:44)
at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1626)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Any ideas how I can solve this warnings?
有什么想法可以解决这个警告吗?
采纳答案by Sebastian Frey
I was able to solve this problem by using HK2's org.glassfish.hk2.api.PerLookupAnnotation instead of Jerseys org.glassfish.jersey.process.internal.RequestScopedAnnotation in my AbstractBinder class when I bind my factory to my interface.
当我将我的工厂绑定到我的接口时,我能够通过在我的 AbstractBinder 类中使用 HK2 的org.glassfish.hk2.api.PerLookupAnnotation 而不是 Jerseys org.glassfish.jersey.process.internal.RequestScopedAnnotation来解决这个问题。
回答by Vitali Pom
You can check if you have a syntax error in one of your methods in the class you cannot "import" with Jersey, for me this was the case. I had two unclosed pluses in a string.
您可以检查您无法使用 Jersey“导入”的类中的方法之一是否存在语法错误,对我来说就是这种情况。我在一个字符串中有两个未闭合的加号。
回答by user3707125
I was able to workaround this bug by injecting Factoryinstead of the direct class. I also tested with HttpServletRequestinjection and it works as well. However no @PathParamor any other smart annotation worked for me, so most likely you will have to workaround their usage.
我能够通过注入Factory而不是直接类来解决这个错误。我还用HttpServletRequest注射进行了测试,它也能正常工作。但是,没有@PathParam或任何其他智能注释对我有用,因此很可能您必须解决它们的使用问题。
My old code:
我的旧代码:
##代码##My new code:
我的新代码:
##代码##
