@Inject 在 JAX-WS 中引发 NullPointerException

@Inject raises NullPointerException in JAX-WS

我正在开发一个简单的 JAX-WS 应用程序。

我的一个 Web 服务端点已注入对象

@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);
  }    
}

当我通过 soap 调用它的 addClient(Client client) 方法时,它引发了 NPE。

我的 DAO 也很简单,看起来像

@Singleton
public class ClientDAO {

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

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

我尝试为 setter 注入注入类型,但它也没有用。 我还尝试删除 @Singleton and/or 使用其他注释,如 @ManagedBean 或 @Stateful,但是,NPE 仍然存在。 我确实有 sun-jaxws.xml 和 2 个端点。 我的项目中确实有 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_1_1.xsd"
bean-discovery-mode="all">
</beans>

如果我将其声明为 CliendDAO dao = new ClientDAO();,那么所有 DAO 方法都可以工作,但这不是我希望我的代码形成的方式。

我的 pom.xml 只有 2 个依赖项:

<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>

请问我该如何解决这个问题?

基本上,我必须从 WEB-INF 文件夹中删除 sun-jaxws.xml。我不知道发生了什么,但它确实产生了影响——启用了 CDI。 现在我的 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_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> 

为了去获取你的项目wsdl可以按照下link: /yourServiceName?wsdl

例如:http://localhost:8080/simplesoap/clients?wsdl 使用 WildFly 或 http://localhost:8080/clients?wsdl 使用 TomEE