当前位置: 首页 > 知识库问答 >
问题:

在不在活动Jersey错误范围内运行的代码中检测到HK2故障

孔瑾瑜
2023-03-14

目前我正在开发一个基于jersey的RESTful应用程序,希望在我的资源中使用DPI。(注意:jersey的版本是2.23.1,servlet容器是tomcat 8.5.3。)

因此,我遵循了教程第23章。在泽西文档中自定义注入和生命周期管理,并创建了一个资源,一个工厂并将工厂绑定到一个类,如下所示:

资源:

@Path("/{project}/catalogs")
public class ProjectsResource {

   @Inject
   Project project;

   ...
}

厂:

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) {}

}

我还有一个功能,注册一个AbstractBinder,将我的ProjectFactory绑定到我的项目类。

@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;
  }

}

实际问题是,当我调用我的资源时,一切都很好,我可以访问我的项目实例,但是在我的tomcat catalina日志中,我得到以下stacke跟踪:

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$asyncGetRequest$0(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$0(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$0(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)

有什么办法可以解决这个警告吗?

共有3个答案

袁山
2023-03-14

你可以检查你不能用Jersey“导入”的类中的方法是否有语法错误,对我来说就是这种情况。我在一根绳子上有两个未闭合的加号。

商宏爽
2023-03-14

我能够通过注入Factory而不是直接类来解决这个bug。我还使用HttpServletRequest注入进行了测试,它也能正常工作。然而,没有@PathParam或任何其他智能注释对我有效,所以很可能您必须解决它们的用法。

我的旧代码:

@Inject
public void setContext(ContainerRequestContext context) {
    this.context = context;
}

@Override
public SomeInfo provide() {
    return (SomeInfo) context.getProperty(SOME_KEY);
}

我的新代码:

@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);
    }
}
卢英叡
2023-03-14

当我将我的工厂绑定到我的接口时,我能够通过在我的AbstractBinder类中使用HK2的org.glassfish.hk2.api.PerLookup注释而不是Jerseysorg.glassfish.jersey.process.internal.RequestScoped注释来解决这个问题。

import 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;
  }

}
 类似资料:
  • 问题内容: unicode对我来说很奇怪。我当时处理的是unicode很好,但是今天早上运行它时,u’\ u201d’一项出现错误并给了我 我查找了代码,显然是它的utf-32,但是当我尝试在解释器中对其进行解码时: 或与此相关的任何其他操作,它只是无法在任何编解码器中识别它,但我发现它是“正确的双引号” 我得到: 问题答案: 你已经有一个unicode字符串,就没有必要将其解码为unicode字

  • 我试图构建这个应用程序,但是Android Studio在文件上扔给我一个编译时错误,告诉我语句结构不正确。当我试图用公共修饰符声明一个方法时,第一个错误是“非法开始表达式”错误。然后,我使用的每个对象都找不到,不管对象是什么(菜单、视图、工具栏等)。 我可以在声明我的AppBarConfiguration之后很好地声明对象,但是我不能调用方法。 我曾尝试在调用onCreate之前添加私有对象并使

  • 我正在与Maven合作,在编译和构建项目时遇到错误。评估一些pmml文件是jpmml项目。现在我发现了这个错误: 规则0:org.apache.maven.plugins.enforcer.要求MavenVersion失败,消息:检测到的Maven版本:3.0.5不在允许的范围3.2内。 检查链接以查看图像:https://www.dropbox.com/s/3r9d8g8l4r1zctp/mav

  • 我使用LeScanCallback(无法使用更新的扫描方法,因为我正在为api 18开发。这并不重要,因为android 5.0 api也不提供此功能)来检测何时检测到附近的BLE设备: 我没有与设备配对或连接,因为这不是必需的,我只是想看看附近有哪些设备。 我正在尝试创建一个服务,每隔5分钟左右,调用一个Web服务器来更新此时附近的设备。 棘手的是android设备将移动,所以现在附近的蓝牙设备

  • 问题内容: [不重复类似的问题,如下面进一步解释的那样] 从Jenkins内部运行XCodeBuild时,出现代码签名错误,但可以从命令行或Xcode内部生成。 过去有几个人遇到过这个问题,他们的共同主题是Jenkins在启动时以守护程序用户身份运行,因此尝试访问系统钥匙串。人们应用的解决方案是将凭据复制到系统钥匙串,或者运行命令来设置要使用的钥匙串。 但是,就我而言,如果我查看活动管理器中的启动

  • 我一直在阅读各种帖子、问题、答案和文档,但到目前为止还没有解决我的问题。 我正在使用mbExtruder jQuery插件,为了将其集成到angular中,我为它创建了一个指令: 我正在提取容器div,编译它,应用scope并用转换后的div替换原来的div。 现在,我使用mbExtruder的功能从单独的HTML文件加载内容,如下所示: 在视图的HTML中,我有以下内容: 我在指令中得到的范围是