gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:56:56 32 4
gpt4 key购买 nike

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

Jersey 是 Java EE 8 自 2.26 版以来的一部分,所以我还决定将我的项目从 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-api , jersey-hk2 , javax.inject , jersey-cdi1x , jersey-cdi1x-servlet , weld-servlet , weld-servlet-core , weld-servlet-shaded , weld-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>

    这是我在尝试注入(inject) @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 启动日志:

    2018 年 6 月 4 日上午 10:09:07 org.jboss.weld.environment.servlet.EnhancedListener onStartup
    信息:WELD-ENV-001008:使用 ServletContainerInitializer 初始化 Weld
    2018 年 6 月 4 日上午 10:09:08 org.jboss.weld.bootstrap.WeldStartup
    信息:WELD-000900:3.0.4(最终)
    2018 年 6 月 4 日上午 10:09:08 org.jboss.weld.bootstrap.WeldStartup startContainer
    信息:WELD-000101:交易服务不可用。 @Inject UserTransaction 的注入(inject)不可用。事务性观察者将被同步调用。
    2018 年 6 月 4 日上午 10:09:09 org.jboss.weld.environment.tomcat.TomcatContainer 初始化
    信息:WELD-ENV-001100:检测到 Tomcat 7+,CDI 注入(inject)将在 Servlet、过滤器和监听器中可用。

    最佳答案

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

    我缺少的一件事是注册 AbstractBinder在我的ResourceConfig ,如 this answer 中所述在 Jersey 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;

    // ...

    }

    另请注意,如果您想将其他可注入(inject)剂注入(inject)到您的可注入(inject)剂中,它们将在构造后生命周期事件之前不可用:
    @Singleton
    public class MyInjectable {

    @Inject
    OtherInjectable other;

    @PostConstruct
    private void init() {

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

    }

    关于jersey - Jersey/CDI 2.0/Weld 3.0 所需的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50677109/

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