CDI injection in JAX-WS endpoint does not work, re

2019-07-31 15:26发布

问题:

Why doesn't the following CDI work in JAX-WS endpoints in glassfish 3.x.x? I get an NPE when accessing the service from the endpoint.

@WebService
public class JaxWsTestEndpoint {

    @Inject
    private MyService service;

    @WebMethod
        public String sayHello(String name) {
        System.out.println("injected service:" + service);
        service.callService();
        return "Hello, " + name + ".";
    }
}

Where the class "service" is defined as follows:

@Named("myService")
public class MyService {
     public MyService() {
        System.out.println("init myService.");
     }

    public void callService() {
            System.out.println("calling Service.");
    }
 }

I have an empty beans.xml file in WEB-INF. I tried it with complete empty content, and with an empty

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">

</beans>

tag. But somehow, the service field in the JAX-WS endpoint is still NULL after deployment and during receiving a web service request, resulting in a NPE when calling the service. What am i missing here?

回答1:

You can try to remove sun-jaxws.xml from WEB-INF directory. This way has helped me!



回答2:

Yes I got it working by removing sun-jaxws.xml and modifying web.xml pointing my webservice directly instead of WSServlet.

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <!-- This listener parses a sun specific configuration file (sun-jaxws.xml), which provides the web service
endpoints and connects the WSServlet instance to the services' implementation classes -->
<!--<listener>
    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener> -->

<!-- Delegate requests whose URLs end with the path '/StakeholderWebService' to a WSServlet instance provided by container, which in turn is linked to the JWS runtime --> 
<servlet>
    <servlet-name>StakeholderWebService</servlet-name>
<!--     <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class> -->
    <servlet-class>com.werner.stakeholder.webservices.StakeholderWebServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>StakeholderWebService</servlet-name>
    <url-pattern>/stakeholderWebService</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>120</session-timeout>
</session-config>



标签: java jax-ws cdi