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

Jersey/CDI 2.0/Weld 3.0的必需依赖项

史修谨
2023-03-14

我尝试过的事情:

  • 我已经了解了在Tomcat上安装Weld的答案
  • 最新的Jersey文档似乎没有涵盖这种情况。无论如何,我已经尝试模仿CDI webapp示例中包含的依赖项
  • 焊接2.4到3.0迁移指南
  • ...最后,让我绝望的是,以下依赖关系的不同组合

以下是我当前的依赖项(pom.xml):

<!-- Java EE  -->
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>${java-ee.version}</version>
</dependency>

<!-- JAX-RS -->
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>${jaxrs.version}</version>
</dependency>

<!-- Jersey -->
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>${jersey.version}</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>${jersey.version}</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>${jersey.version}</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey.version}</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>${jersey.version}</version>
</dependency>

<!--  CDI (Jersey HK2 / Weld) -->
<dependency>
    <groupId>org.glassfish.jersey.inject</groupId>
    <artifactId>jersey-hk2</artifactId>
    <version>${jersey.version}</version>
</dependency>
<dependency>
    <groupId>org.jboss.weld.servlet</groupId>
    <artifactId>weld-servlet-shaded</artifactId>
    <version>${weld.version}</version>
</dependency>
<dependency>
    <groupId>org.jboss.weld</groupId>
    <artifactId>weld-core-impl</artifactId>
    <version>${weld.version}</version>
</dependency>

当我试图将@singleton注入到我的资源(@path)中时,这就是我当前遇到的错误:

ADVERTENCIA: The following warnings have been detected: WARNING: Unknown HK2 failure detected:

...

GRAVE: El Servlet.service() para el servlet [CGERestApplication] en el contexto con ruta [/igea-cge-interfaces] lanzó la excepción [A MultiException has 3 exceptions.  They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=SapClient,parent=CambioEstadoTrabajoService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,363959695)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of es.indra.isl.igea.external.cge.web.api.services.sap.CambioEstadoTrabajoService errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on es.indra.isl.igea.external.cge.web.api.services.sap.CambioEstadoTrabajoService
] with root cause:
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=SapClient,parent=CambioEstadoTrabajoService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,363959695)
    at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:75)
    at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:212)
    at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:235)
    at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:358)
    at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487)
    at org.glassfish.jersey.inject.hk2.RequestContext.findOrCreate(RequestContext.java:83)
    at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2126)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:777)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:740)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:710)
    at org.glassfish.jersey.inject.hk2.AbstractHk2InjectionManager.getInstance(AbstractHk2InjectionManager.java:184)
    at org.glassfish.jersey.inject.hk2.ImmediateHk2InjectionManager.getInstance(ImmediateHk2InjectionManager.java:54)
    at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:129)
    at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:284)
    at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:75)
    at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:110)

和Tomcat启动日志:

jun 04, 2018 10:09:07 AM org.jboss.weld.environment.servlet.EnhancedListener onStartup
INFO: WELD-ENV-001008: Initialize Weld using ServletContainerInitializer
jun 04, 2018 10:09:08 AM org.jboss.weld.bootstrap.WeldStartup 
INFO: WELD-000900: 3.0.4 (Final)
jun 04, 2018 10:09:08 AM org.jboss.weld.bootstrap.WeldStartup startContainer
INFO: WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
jun 04, 2018 10:09:09 AM org.jboss.weld.environment.tomcat.TomcatContainer initialize
INFO: WELD-ENV-001100: Tomcat 7+ detected, CDI injection will be available in Servlets, Filters and Listeners.

共有1个答案

阚元白
2023-03-14

经过更多的研究,我发现我在问题中列出的依赖关系是正确的。

我缺少的一件事是在我的ResourceConfig中注册AbstractBinder,如本答案和Jersey用户指南中所述。

示例

public class MyApplication extends ResourceConfig {

    public MyApplication() { 
        super();    

        // ...

        register(new MyInjectionBinder());
    }
}


@Singleton
public class MyInjectable {
    // ...
}


public class MyInjectionBinder extends AbstractBinder {

    @Override
    protected void configure() {
        bind(MyInjectable.class).to(MyInjectable.class).in(Singleton.class);
    }
}


@Path("/myresource")    
public class MyResource {

    @Inject
    MyInjectable injectable;

    // ...

}
@Singleton
public class MyInjectable {

    @Inject
    OtherInjectable other;

    @PostConstruct
    private void init() {   

        // ... do something with "other"
    }

}
 类似资料:
  • 我对React这个话题还不熟悉。我最近启动了它,但当我想启动React应用程序时遇到了一个问题“纱线开始”显示此错误: 请问我怎样才能克服这个错误。 我试图在网上找到解决方案,但我并不幸运。请问问题出在哪里?

  • 我能够成功运行所有使用pandas的脚本,但突然我所有的pandas脚本都出现以下错误: 回溯(最近一次呼叫最后一次): 文件“data_visualization.py”,第5行,在 文件“/usr/lib64/python2.7/site packages/pandas/init.py”,第18行,在 最近我没有安装或更新任何新东西。 有人对此有解决办法吗? 我卸载了熊猫和Numpy,并重新安

  • 从昨天开始,我尝试在anaconda上导入包时出现以下错误: 我尝试过卸载Anaconda和Python,切换到Python 2.7,但没有任何效果仍然是相同的错误,下面是我得到的代码: 非常感谢您的帮助,谢谢!

  • 问题内容: 从头开始,在没有任何Jersey 1.x知识的情况下,我很难理解如何在Jersey 2.0项目中设置依赖项注入。 我也知道HK2在Jersey 2.0中可用,但是我似乎找不到帮助Jersey 2.0集成的文档。 pom.xml 我可以让容器启动并使用我的资源,但是一旦我将@Inject添加到MyService,框架就会引发异常: 问题答案: 你需要定义一个并将其注册到你的JAX-RS应

  • Jersey 的创建、组装和安装都是使用 Apache Maven,非快照的 Jersey 都部署到了 Maven 中央库。他也部署在了Java.Net Maven repositories,包括带有快照的版本。当然如果要查看最新的版本也可以从 Java.Net Maven repositories 检出。 一个使用 Jersey 的应用,依赖于 Jersey 的模块,但是如果使用了第三方模块,那

  • 问题内容: 我已经从Hibernate网站下载了zip文件,我们有一个包含所有必需jar的文件夹。 但是我想和Maven在一起。我需要检查哪些是此Hibernate版本所需的库,然后在中手动添加它们吗? 有没有办法只添加hibernate和Maven来添加所有必需的库本身? 问题答案: 如果要将JPA与Hibernate一起使用,则只需一个Maven依赖项。请参考下载页面: 这种依赖关系会将所有必