Reputation: 113
I'm trying to get CDI to work in RESTEasy resources in Tomcat 10.1.17, but it eludes me.
<Context>
<Resource name="BeanManager"
auth="Container"
type="jakarta.enterprise.inject.spi.BeanManager"
factory="org.jboss.weld.resources.ManagerObjectFactory"/>
</Context>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
metadata-complete="false"
version="5.0">
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
<!-- RESTEasy stuff -->
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-cdi -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-cdi</artifactId>
<version>6.2.8.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>6.2.8.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jackson2-provider -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>6.2.8.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-servlet-initializer -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>6.2.8.Final</version>
</dependency>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>4.0.1</version>
</dependency>
<!-- CDI Implementation: -->
<!-- https://mvnrepository.com/artifact/org.jboss.weld.servlet/weld-servlet-core -->
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-shaded</artifactId>
<version>5.1.2.Final</version>
</dependency>
Logs output:
[INFO] k8s: 10-May-2024 11:13:41.605 INFO [main] org.jboss.weld.environment.servlet.EnhancedListener.onStartup WELD-ENV-001008: Initialize Weld using ServletContainerInitializer
[INFO] k8s: 10-May-2024 11:13:41.613 INFO [main] org.jboss.weld.bootstrap.WeldStartup.<clinit> WELD-000900: 5.1.2 (Final)
[INFO] k8s: 10-May-2024 11:13:41.652 INFO [main] org.jboss.weld.environment.deployment.discovery.DiscoveryStrategyFactory.create WELD-ENV-000020: Using jandex for bean discovery
[INFO] k8s: 10-May-2024 11:13:41.711 INFO [main] org.jboss.weld.bootstrap.WeldStartup.startContainer WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
[INFO] k8s: 10-May-2024 11:13:41.793 INFO [main] org.jboss.weld.environment.tomcat.TomcatContainer.initialize WELD-ENV-001100: Tomcat 7+ detected, CDI injection will be available in Servlets, Filters and Listeners.
When I implement a listener with "@WebListener" and annotated a field with "@Inject", it works.
However, "@Inject" doesn't work for fields inside Resource class (annotated with "@Path") or for Application class (annotated with "@ApplicationPath").
I did put a dependency for resteasy-cdi in the pom.xml file. According to RESTEasy docs this should be it ?!
What am I missing here to make CDI work in RESTEasy ?
Upvotes: 1
Views: 119