球衣 3 与 CDI Weld 4 而不是 HK2

Jersey 3 with CDI Weld 4 instead of HK2

我在独立 Jetty 11 中有一个小型 JAX-RS(Jersey 3.0.1)API 运行,但我想使用 Weld 4.0.0 而不是 HK2。 Jetty 正确引导 Weld 上下文(Jetty 模块 ci-decorate 按照 Weld documentation 中的描述安装)

WELD-ENV-001212: Jetty CdiDecoratingListener support detected, CDI injection will be available in Listeners, Servlets and Filters.

调用API端点时发生异常:

WARNING: The following warnings have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 1
org.jboss.weld.exceptions.IllegalStateException: WELD-001304: More than one context active for scope type jakarta.enterprise.context.RequestScoped
    at org.jboss.weld.manager.BeanManagerImpl.internalGetContext(BeanManagerImpl.java:672)
    at org.jboss.weld.manager.BeanManagerImpl.isContextActive(BeanManagerImpl.java:658)
    at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:163)
    at org.jboss.weld.contexts.unbound.DependentContextImpl.get(DependentContextImpl.java:64)
    at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:100)
    at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
    at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:694)
    at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:717)
    at org.jboss.weld.util.ForwardingBeanManager.getReference(ForwardingBeanManager.java:64)
    at org.jboss.weld.bean.builtin.BeanManagerProxy.getReference(BeanManagerProxy.java:87)
    at org.glassfish.jersey.ext.cdi1x.internal.CdiUtil.getBeanReference(CdiUtil.java:127)
    at org.glassfish.jersey.ext.cdi1x.internal.AbstractCdiBeanSupplier.getInstance(AbstractCdiBeanSupplier.java:69)
    at org.glassfish.jersey.ext.cdi1x.internal.AbstractCdiBeanSupplier._provide(AbstractCdiBeanSupplier.java:103)
    at org.glassfish.jersey.ext.cdi1x.internal.RequestScopedCdiBeanSupplier.get(RequestScopedCdiBeanSupplier.java:46)
    at org.glassfish.jersey.inject.hk2.InstanceSupplierFactoryBridge.provide(InstanceSupplierFactoryBridge.java:53)
    at org.jvnet.hk2.internal.FactoryCreator.create(FactoryCreator.java:129)

我在 pom.xml 中的依赖项如下所示:

        <dependency>
            <groupId>org.jboss.weld.servlet</groupId>
            <artifactId>weld-servlet-core</artifactId>
            <version>4.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>5.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>jakarta.ws.rs</groupId>
            <artifactId>jakarta.ws.rs-api</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext.cdi</groupId>
            <artifactId>jersey-weld2-se</artifactId>
            <version>3.0.1</version>
        </dependency>

对于 Weld 上下文,文件夹 webapp/WEB-INF/beans.xml[=34= 中存在空的 beans.xml ] 用 bean-discovery-mode="all" 声明。 接下来显示 JAX-RS 端点 class:

import de.test.weld.service.AddressRepository;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("address")
public class AddressApi {

    @Inject
    private AddressRepository addressRepository;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String getAddress() {
        return "{\"street\": \"" + addressRepository.getStreet(0) + "\", \"postcode\":  \"" +
                addressRepository.getPostcode(0) + "\", \"city\": \"" + addressRepository.getCity(0) + "\"}";
    }
}

我要注入的bean AddressRepositoryImpl 注释为@jakarta.inject.Singleton

我将不胜感激任何提示,以便它与 CDI Weld 一起使用。

问题是错误的依赖关系

<dependency>
   <groupId>org.glassfish.jersey.ext.cdi</groupId>
   <artifactId>jersey-weld2-se</artifactId>
   <version>3.0.1</version>
</dependency>

仅适用于非 JEE 环境,即 Java 标准版 (SE) 或非网络应用程序。

然后仍然缺少以下依赖项:

 <dependency>
     <groupId>org.glassfish.jersey.ext.cdi</groupId>
     <artifactId>jersey-cdi1x</artifactId>
     <version>3.0.1</version>
 </dependency>

解决方案参考:Jersey Github Issues