I have a small JAX-RS (Jersey 3.0.1) API running in a standalone Jetty 11 but want to use Weld 4.0.0 instead of HK2. The Jetty bootstraps the Weld context properly (Jetty module ci-decorate
was installed as described in the Weld documentation)
WELD-ENV-001212: Jetty CdiDecoratingListener support detected, CDI injection will be available in Listeners, Servlets and Filters.
When it comes to call the API endpoint an exception occurs:
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)
My dependencies in the pom.xml look as follows:
<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>
For the Weld context there exists an empty beans.xml in folder webapp/WEB-INF/beans.xml declared with bean-discovery-mode="all"
.
The JAX-RS endpoint class is shown next:
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) + "\"}";
}
}
The bean AddressRepositoryImpl
which I want to inject is annotated with @jakarta.inject.Singleton
.
I would appreciate any hint so it works with CDI Weld.
The problem was the wrong dependency
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-weld2-se</artifactId>
<version>3.0.1</version>
</dependency>
which exists only for non-JEE environments, i.e. Java Standard Edition (SE) or non-webapps.
Then the following dependency was still missing:
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-cdi1x</artifactId>
<version>3.0.1</version>
</dependency>
Solution refers to: Jersey Github Issues