gpt4 book ai didi

java - 带有 CDI Weld 4 而非 HK2 的 Jersey 3

转载 作者:行者123 更新时间:2023-12-04 07:57:15 24 4
gpt4 key购买 nike

我有一个小型 JAX-RS (Jersey 3.0.1) API 在独立的 Jetty 11 中运行,但想使用 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$1.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 上下文,在使用 bean-discovery-mode="all" 声明的文件夹 webapp/WEB-INF/beans.xml 中存在一个空 beans.xml .
JAX-RS 端点类如下所示:
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我要注入(inject)的注解为 @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) 或非 webapps。
然后仍然缺少以下依赖项:
 <dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-cdi1x</artifactId>
<version>3.0.1</version>
</dependency>
解决方案引用: Jersey Github Issues

关于java - 带有 CDI Weld 4 而非 HK2 的 Jersey 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66639421/

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