3

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?

  • my pom.xml has got only 2 dependencies: com.sun.xml.ws jaxws-rt 2.1.3 javax javaee-api 7.0 – Ivan Kravchenko Oct 25 '16 at 07:52
  • take a look at [this SO post](http://stackoverflow.com/questions/5661022/inject-ed-attribute-remains-null?rq=1). It seems that this user had the same problem. Maybe it will help you. **EDIT:** and try to put your `clientDAO.addClient(client);` into an `else` block. Otherwise that code gets executed even if `clientDAO == null`. – QBrute Oct 25 '16 at 07:54
  • 1
    Could you post the stacktrace also? – Duong Nguyen Oct 25 '16 at 07:56
  • This is my stacktrace: http://txt.do/d5q1g – Ivan Kravchenko Oct 25 '16 at 08:01
  • 1
    Do you use Tomcat as servlet container? (I've seen catalina classes in the stacktrace.) Tomcat ist not a full-blown EE container and you have to add an implementation of the CDI api. For example weld. – Neothorn Oct 25 '16 at 08:28
  • I actually tried this on both TomEE and WildFly, doens't work on either – Ivan Kravchenko Oct 25 '16 at 08:43
  • Can you try annotating your WS with `@Stateless`. Also where is your beans.xml ? It must be in `META-INF` for jars, and `WEB-INF` for wars – Svetlin Zarev Oct 25 '16 at 08:56
  • beans.xml is located in WEB-INF folder. Annotating web service with @Stateless doesn't work. – Ivan Kravchenko Oct 25 '16 at 09:07
  • Lose the `jaws-rt` dependency. The API and implementation for this is already provided by Java 6 and newer. That said, as already mentioned you must deploy to a full Java EE implementation in order for injection into @WebService annotated classes to work. – Steve C Oct 25 '16 at 13:55
  • I cant delete that since jaxws-rt links with WSServletContextListener and WSServlet in my web.xml – Ivan Kravchenko Oct 25 '16 at 14:08
  • You don't need anything in your web.xml (if you're deploying to TomEE or WildFly) – Steve C Oct 26 '16 at 12:43
  • How am I to declare servlets and their mappings then? – Ivan Kravchenko Oct 26 '16 at 20:28

1 Answers1

0

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