0

I want to add CDI to Tomcat 9 since this servlet container does not support CDI. The main idea of this project is to create a REST website in Java without the Spring Framework in order to improve and learn core mechanics and JavaEE.

I went through this answer:

and I have chosen not to use TomEE and continue with Tomcat 9.

I could not make it work as expected after following the tutorial since the injected class remains null, throwing a NullExceptionError when calling a method from the Servlet.

Servlet.service() for servlet [ApiServlet] throws Exception: NullExceptionError

From debugging the code, I see the Injected class is null as show in this image below.

Debug confirmation of null

In order to be able to use CDI with tomcat (following the previous answer) I ended with the next setup:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cca</groupId>
  <artifactId>dadas</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>CDI_SPM_Tomcat</name>
  <description>dsadas</description>
  
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  
  <dependencies>
     <!-- Servlet -->
     <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.0</version>
        <scope>provided</scope>
     </dependency>
 <!-- JSF API -->
     <dependency>
        <groupId>javax.faces</groupId>
        <artifactId>javax.faces-api</artifactId>
        <version>2.3</version>
     </dependency> 
     <!-- CDI -->
     <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>2.0</version>
     </dependency>   
     <!--  To be able to CDI with Tomcat -->
    <dependency>
        <groupId>org.jboss.weld.servlet</groupId>
        <artifactId>weld-servlet</artifactId>
        <version>2.2.9.Final</version>
    </dependency>  

  </dependencies>
</project>

A beans.xml is needed for the mapping

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       version="1.2" bean-discovery-mode="annotated">

    <!-- some content -->
</beans>

From reading docs I found that adding this <resource-env-ref> is needed. If you use a .jar file, then add a Listener (commented).

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0"> 
  
   <!-- <listener>
        <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
    </listener> -->
  
    <resource-env-ref>
       <resource-env-ref-name>BeanManager</resource-env-ref-name>
       <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
     </resource-env-ref>

</web-app>

To test the idea and knowledge I went with a quick reproducible test which consists of a class

TestClass

@FacesConfig
@RequestScoped
public class TestClass {
    
    public TestClass() {
        // TODO Auto-generated constructor stub
    }
    
    public String getString() {
        return "String";
    }

}

and the servlet where I am trying to inject the class

Servlet

@WebServlet("/test")
public class ApiServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    @Inject
    private TestClass testClass;
    
    
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ApiServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        
        System.out.println(testClass.getString());
        
        
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

0 Answers0