代码之家  ›  专栏  ›  技术社区  ›  Eternal Rubyist

JAX-WS WebServiceContext保持为null(注释注入失败)

  •  3
  • Eternal Rubyist  · 技术社区  · 12 年前

    我已经尝试将我的应用程序部署到带有Metro/Zerosey和Glassfish 3.1.2的Tomcat 6,但访问WebServiceContext资源总是会导致空指针异常, 除了 当我使用自动生成的Glassfish测试门户测试应用程序时。

    下面是我写的一个简单的测试方法来验证这一点:

    import javax.annotation.Resource;
    import javax.jws.WebService;
    import javax.xml.ws.WebServiceContext;
    import javax.xml.ws.handler.MessageContext;
    import javax.servlet.ServletContext;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    
    @WebService
    @Path("/test")
    public class Test {
        @Resource
        WebServiceContext wsContext;
    
        @GET
        @Produces("text/plain")
        @Path("/hello")
        public String hello() {
            MessageContext mc = wsContext.getMessageContext();   // NULL POINT HAPPENS HERE!
            ServletContext servletContext = (ServletContext) 
                    mc.get(
                            MessageContext.SERVLET_CONTEXT);
            String s = servletContext.getRealPath("/WEB-INF");
            return "Real Path: " + s;
        }
    }
    

    这里是相应的web xml(主要由Eclipse自动生成):

    <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_2_5.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>WebApp</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <servlet>
        <description>JAX-RS Tools Generated - Do not modify</description>
        <servlet-name>JAX-RS Servlet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>JAX-RS Servlet</servlet-name>
        <url-pattern>/jaxrs/*</url-pattern>
      </servlet-mapping>
    </web-app>
    

    最有趣的是,当我通过访问Glassfish生成的URL来测试TestService时:

    http://localhost:8080/WebApp/TestService?Tester
    

    我可以通过提交表单来测试/调用“hello”方法,并获得一个生成适当文件路径的有效WebServiceContext对象。不幸的是,实际执行的HTTP GET请求

    $curl -H "Accept: text/plain" http://localhost:8080/WebApp/jaxrs/test/hello
    

    导致指向Test类中的第22行(WebServiceContext)的空指针堆栈跟踪。

    为什么web服务在Glassfish测试工具中工作,而不是从实际的HTTP请求中工作?

    更新:

    我能够确认,如果您使用GlassFish默认的XMLSOAP请求到GlassFish的默认URL,那么Web服务可以完美地工作(WebServiceContext被正确地注入),如下所示:

    $curl -X POST -d @req.xml http://localhost:8080/ZlotyGlassfish/TestService --header "Content-Type:text/xml" 
    

    其中文件“req.xml”的内容是与WSDL定义匹配的请求:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:impl="http://impl.rest.webservice.webapp.com/">
       <soapenv:Header/>
       <soapenv:Body>
          <impl:hello/>
       </soapenv:Body>
    </soapenv:Envelope>
    

    我的主要问题仍然是,为什么这能通过默认的GlassFish实现完美地工作,但@GET和@Path符号生成的端点却无法注入适当的WebServiceContext对象?

    1 回复  |  直到 12 年前
        1
  •  5
  •   Lan    12 年前

    不同之处在于,默认的GlassFish实现是JAX-WS 基于SOAP web服务。对于JAX-RSWeb服务,您应该使用以下注释来注入上下文。

     @Context 
     private ServletContext sc;
    

    您可以使用@Context注释来注入其他类型,例如 UriInfo , HttpHeaders 。请参阅 Jersey documentation 详细信息。 WebServiceContext MessageContext 是在JAX-RS上下文中没有意义的JAX-WS对象。