I followed this tutorial to setup weld on tomcat. I'm using Netbeans 12 IDE and project is web application with ant. Running tomcat is 9.0.50. To my project I added dependency weld-servlet-shaded-3.1.8.Final. Then created simple bean:
package com.example.app;
import javax.annotation.ManagedBean;
@ManagedBean
public class SomeObject {
public String sayHello(){
return "heloo from some object :)";
}
}
This is simple web service which is using defined bean:
package com.example.app;
import javax.inject.Inject;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
@WebService(serviceName = "MyService")
public class MyService {
@Inject
private SomeObject object;
@WebMethod(operationName = "hello")
public String hello(@WebParam(name = "name") String txt) {
return "Hello " + object.sayHello() + " !";
}
}
WEB-INF/beans.xml:
<?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_2_0.xsd"
version="2.0"
bean-discovery-mode="all">
</beans>
WEB-INF/web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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/web-app_3_1.xsd">
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>MyService</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyService</servlet-name>
<url-pattern>/MyService</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
META-INF/context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/WebAppJAXWS">
<Resource name="BeanManager"
auth="Container"
type="javax.enterprise.inject.spi.BeanManager"
factory="org.jboss.weld.resources.ManagerObjectFactory" />
</Context>
Weld is successfully started by tomcat:
07-Sep-2021 10:48:33.429 INFO [http-nio-8080-exec-60] org.apache.catalina.startup.HostConfig.deployDescriptor Deploying deployment descriptor [C:\tomcat\apache-tomcat-9.0.50\conf\Catalina\localhost\WebAppJAXWS.xml]
07-Sep-2021 10:48:33.430 WARNING [http-nio-8080-exec-60] org.apache.catalina.startup.HostConfig.deployDescriptor The path attribute with value [/WebAppJAXWS] in deployment descriptor [C:\tomcat\apache-tomcat-9.0.50\conf\Catalina\localhost\WebAppJAXWS.xml] has been ignored
07-Sep-2021 10:48:34.658 INFO [http-nio-8080-exec-60] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
07-Sep-2021 10:48:34.678 INFO [http-nio-8080-exec-60] org.jboss.weld.environment.servlet.EnhancedListener.onStartup WELD-ENV-001008: Initialize Weld using ServletContainerInitializer
07-Sep-2021 10:48:34.706 INFO [http-nio-8080-exec-60] org.jboss.weld.bootstrap.WeldStartup.<clinit> WELD-000900: 3.1.8 (Final)
07-Sep-2021 10:48:34.958 INFO [http-nio-8080-exec-60] org.jboss.weld.bootstrap.WeldStartup.startContainer WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
07-Sep-2021 10:48:35.193 INFO [http-nio-8080-exec-60] org.jboss.weld.environment.tomcat.TomcatContainer.initialize WELD-ENV-001100: Tomcat 7+ detected, CDI injection will be available in Servlets, Filters and Listeners.
07-Sep-2021 10:48:35.349 INFO [http-nio-8080-exec-60] com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextInitialized WSSERVLET12: JAX-WS context listener initializing
07-Sep-2021 10:48:36.620 INFO [http-nio-8080-exec-60] com.sun.xml.ws.server.MonitorBase.createRoot Metro monitoring rootname successfully set to: com.sun.metro:pp=/,type=WSEndpoint,name=/WebAppJAXWS-MyService-MyServicePort
07-Sep-2021 10:48:36.771 INFO [http-nio-8080-exec-60] com.sun.xml.ws.transport.http.servlet.WSServletDelegate.<init> WSSERVLET14: JAX-WS servlet initializing
07-Sep-2021 10:48:36.786 INFO [http-nio-8080-exec-60] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of deployment descriptor [C:\tomcat\apache-tomcat-9.0.50\conf\Catalina\localhost\WebAppJAXWS.xml] has finished in [3,356] ms
When I call my web method exception is returned that injected object is null:
07-Sep-2021 10:52:00.218 SEVERE [http-nio-8080-exec-63] com.sun.xml.ws.server.sei.EndpointMethodHandler.invoke null
java.lang.NullPointerException
at com.example.app.MyService.hello(MyService.java:16)
...
What might be wrong in this configuration?