gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:03:45 27 4
gpt4 key购买 nike

我尝试将我的应用程序部署到 Tomcat 6 with Metro/Jersey 和 Glassfish 3.1.2,但是访问 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 默认 XML SOAP 请求向 GlassFish 默认 URL 发送请求,我可以确认 Web 服务工作正常(WebServiceContext 已正确注入(inject)),如下所示:

$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 表示法生成的端点无法注入(inject)适当的 WebServiceContext 对象?

最佳答案

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

 @Context 
private ServletContext sc;

还有其他类型可以使用@Context注解来注入(inject),例如UriInfoHttpHeaders。请参阅Jersey documentation了解详情。 WebServiceContextMessageContext 是 JAX-WS 对象,在 JAX-RS 上下文中没有任何意义。

关于web-services - JAX-WS WebServiceContext 保持为 null(注释注入(inject)失败),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15346630/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com