Ivan Kravchenko Ivan Kravchenko - 1 month ago 17
Java Question

@Inject raises NullPointerException in JAX-WS

I'm developing a simple JAX-WS application.

One of my web services endpoints has got injected object

@WebService(
endpointInterface = "com.kravchenko.service.ClientService",
targetNamespace = "http://com.kravchenko/wsdl"
)
@Named("clientServiceImpl")
public class ClientServiceImpl implements ClientService {

@Inject
ClientDAO clientDAO;


public void addClient(Client client) {
if (clientDAO == null) {
System.out.println("NULL CLIENTDAO");
}
clientDAO.addClient(client);
}
}


When I'm calling its addClient(Client client) method over soap it raises NPE.

My DAO is also very simple and looks like

@Singleton
public class ClientDAO {

public Map<Long,Client> clients= new ConcurrentHashMap<Long, Client>();;

public void addClient(Client client) {
clients.put(client.getId(),client);
}
}


I tried to injection type for setter injection, but it didn't work, too.
I also tried to remove @Singleton and/or use other annotations like @ManagedBean or @Stateful, however , NPE still remains.
I do have sun-jaxws.xml with 2 endpoints.
I do have beans.xml in my project:

<?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"
bean-discovery-mode="all">
</beans>


All DAO methods work though if I declare it as
CliendDAO dao = new ClientDAO();
But it's not the way I want my code to be formed.

my pom.xml has got only 2 dependencies:

<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>


Any idea how can I solve this problem, please?

Answer

Basically, I had to remove sun-jaxws.xml from WEB-INF folder. I have no idea what happened, but it definitely had impact - CDI became enabled. Now my web.xml looks like:

      <?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_3_0.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">

   <display-name>soap</display-name>
     <listener>
        <listener-class>
          com.sun.xml.ws.transport.http.servlet.WSServletContextListener
        </listener-class>
     </listener>

   <servlet>
     <servlet-name>wsdl</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>wsdl</servlet-name>
       <url-pattern>/clients</url-pattern>
  </servlet-mapping>

</web-app> 

In order go obtain your project wsdl you can follow the next link: /yourServiceName?wsdl

For example: http://localhost:8080/simplesoap/clients?wsdl using WildFly or http://localhost:8080/clients?wsdl using TomEE

Comments