CDI injection in JAX-WS endpoint does not work, results in NPE

倾然丶 夕夏残阳落幕 提交于 2019-12-14 02:43:00

问题


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>



来源:https://stackoverflow.com/questions/8522999/cdi-injection-in-jax-ws-endpoint-does-not-work-results-in-npe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!