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

时间:2018-06-04 09:13:35

标签: jersey cdi weld

我正在尝试将我的Jersey项目从2.25更新为 2.27

Jersey自2.26版以来就是Java EE 8的一部分,所以我还决定将我的项目从Java EE 7更新为 Java EE 8 ,以及从JAX-RS 2.0 / CDI 1。 x / Weld2.x / Apache Tomcat 8到 JAX-RS 2.1 / CDI 2.0 / Weld 3.0 / Apache Tomcat 9

问题是我无法弄清我需要包含在项目中的确切依赖(Maven项目,pom.xml)。

我尝试过的事情:

  • 我已按照this answer上的说明在Tomcat上安装Weld
  • 最新的Jersey documentation似乎不包括这种情况。无论如何,我试图模仿CDI webapp example
  • 中包含的依赖项
  • Weld 2.4 to 3.0 migration guide
  • ...最后,在我的绝望中,以下依赖项的不同组合:cdi-apijersey-hk2javax.injectjersey-cdi1xjersey-cdi1x-servletweld-servletweld-servlet-coreweld-servlet-shadedweld-core-impl

这些是我当前的依赖项(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.

2 个答案:

答案 0 :(得分:0)

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

我遗失的一件事是在我的AbstractBinder中注册ResourceConfig,如this answerJersey user guide中所述。

示例:

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;

    // ...

}

另请注意,如果您想将其他注射剂注入您的注射剂,那么在 postconstruct 生命周期事件之前它们将无法使用:

@Singleton
public class MyInjectable {

    @Inject
    OtherInjectable other;

    @PostConstruct
    private void init() {   

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

}

答案 1 :(得分:0)

我解决了这个问题。我正在使用Jetty 9.4.14,Jersey 2.28和Weld(servlet着色)3.1.0。这是Java 11依赖项的列表。

//Jersey:
jaxb-api-2.3.0.jar
jaxb-core-2.3.0.1.jar
jaxb-impl-2.3.0.1.jar
javax.ws.rs-api-2.1.1.jar
javax.activation-1.2.0.jar
jersey-container-jetty-http-2.28.jar
jersey-container-servlet-core-2.28.jar
jersey-server-2.28.jar
jersey-common-2.28.jar
jersey-client-2.28.jar
//Jersey + CDI:
jersey-bean-validation-2.28.jar
bean-validator-2.5.0-b06.jar
jersey-cdi1x-2.28.jar
jersey-cdi1x-servlet-2.28.jar
jersey-hk2-2.28.jar
hk2-api-2.5.0.jar
hk2-utils-2.5.0.jar
hk2-locator-2.5.0.jar

您可以在泽西岛issue中找到更多信息。