JAX-WS WebServiceContext保持为空(注释注入失败) [英] JAX-WS WebServiceContext remains null (Annotation Injection Fails)

查看:127
本文介绍了JAX-WS WebServiceContext保持为空(注释注入失败)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将我的应用程序部署到Metro / Jersey和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);
返回真实路径:+ s;






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

 < web-app xmlns:xsi =http://www.w3.org/ 2001 / XMLSchema-instancexmlns =http://java.sun.com/xml/ns/javaeexmlns:web =http://java.sun.com/xml/ns/javaee/web-app_2_5。 xsdxsi:schemaLocation =http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsdid =WebApp_ID版本= 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工具 - 不要修改< / description>
< servlet-name> JAX-RS Servlet< / servlet-name>
< servlet-class> com.sun.jersey.spi.container.servlet.ServletContainer< / servlet-class>
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 

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

  $ curl -HAccept:text / plainhttp:// localhost: 8080 / WebApp / jaxrs / test / hello 

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



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



更新:

我能够确认Web服务完美工作(WebServiceContext已正确注入)if您将GlassFish默认XML SOAP请求用于GlassFish默认URL,如下所示:

  $ curl -X POST -d @req。 xml http:// localhost:8080 / ZlotyGlassfish / TestService --headerContent-Type:text / xml

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

 < soapenv:信封xmlns:soapenv =http:// schem as.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对象?

解决方案

区别在于默认的GlassFish实现是基于JAX-WS 基于SOAP的 Web服务。对于JAX-RS Web服务,您应该使用以下注释来注入上下文。

  @Context $ b $ private ServletContext sc; 

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


I've tried deploying my app to both Tomcat 6 with Metro/Jersey and Glassfish 3.1.2, but accessing the WebServiceContext resource always causes a null pointer exception, except when I test the app using the autogenerated Glassfish testing portal.

Here is a simple test method I wrote to verify this:

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

and here is the corresponding web-xml (largely auto-generated by 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>

What's most interesting is that when I test the TestService by going to the Glassfish generated URL:

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

I can test/invoke the "hello" method by submitting the form and get a valid WebServiceContext object that generates the appropriate filepath. Unfortunately, actually performing an HTTP GET Request like

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

results in a null pointer stack trace that points to line 22 (the WebServiceContext) in the Test class.

Why does the web service work in the Glassfish test harness, but not from actual HTTP requests?

UPDATE:

I was able to confirm that the Web Service works perfectly (WebServiceContext is properly injected) if you utilize the GlassFish default XML SOAP request to the GlassFish default URL like this:

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

where the contents of the file "req.xml" are a request matching the WSDL definition:

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

My primary question remains why does this work perfectly through the default GlassFish implementation, but the Endpoints generated by the @GET and @Path notations fail to get injected with the the appropriate WebServiceContext object?

解决方案

The difference is that the default GlassFish implementation is a JAX-WS SOAP-based web service. For a JAX-RS web service, you should use the following annotation to inject the context.

 @Context 
 private ServletContext sc;

There are other types that you can use @Context annotation to inject, such as UriInfo, HttpHeaders. Please see Jersey documentation for details. WebServiceContext and MessageContext are JAX-WS objects that have no meaning in the JAX-RS context.

这篇关于JAX-WS WebServiceContext保持为空(注释注入失败)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆