- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试将我的应用程序部署到 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),例如UriInfo
、HttpHeaders
。请参阅Jersey documentation了解详情。 WebServiceContext
和 MessageContext
是 JAX-WS 对象,在 JAX-RS 上下文中没有任何意义。
关于web-services - JAX-WS WebServiceContext 保持为 null(注释注入(inject)失败),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15346630/
我有两个使用 JAX-WS 实现的 Web 服务:RequestService 和 ExtendedRequestService。 ExtendedRequestService 调用 RequestS
我想知道使用基本身份验证的 HTTP 请求的用户名和密码。 我正在使用 WebServiceContext。 @Resource private WebServiceContext conte
在我的 Web 服务中,我有: @WebService(serviceName = "myservice") public class ServiceName{ @Resource p
我知道之前有人问过这个问题,但没有一个解决方案对我有用。可能是因为他们主要使用符合 EE 的应用程序服务器,这不是我的情况。 我在 jetty 上运行我的网络服务(通过 Eclipse)仅用于开发。但
我正在尝试在我的网络服务中使用 ServletContext,但是 WebServiceContext 始终是 NULL。谁能告诉我我做错了什么,我正在使用 spring 框架和 tomcat。 pa
我正在创建一个使用相互身份验证的 Web 服务(和测试客户端)。我用过 this guide创建 CA 和 key /信任库,但我没有为客户端创建 .p12 文件,而是使用“设置您的 Web 服务器”
我有一个带有 4 个 WebMethods 的 Wildfly 8.1.0 WebService。 我需要记录 SOAP 消息(业务需求) 我注入(inject)了有效的 WebServiceCont
我正在尝试使用 @Resource 注释设置 CXF Apache Web 服务的上下文。但是上下文总是空的。请帮助克服这个问题。下面是我正在尝试访问 messageContext 的网络服务。 @W
我正在关注this guide构建一个名为 [Entando][2] 的应用程序。 特别是,我使用 maven 命令执行了 2.2 中的步骤来构建应用程序 mvn archetype:generate
我有一些 EJB 作为 JAX-WS Web 服务: @WebService @Stateless @Remote(MobileFacade.class) public class MobileFac
我有一个 Metro jax-ws 网络服务,看起来或多或少像这样: @WebService @Transactional public class UserManagementServiceImpl
我尝试将我的应用程序部署到 Tomcat 6 with Metro/Jersey 和 Glassfish 3.1.2,但是访问 WebServiceContext 资源总是会导致空指针异常,除了当我使
我使用的是 SAP NetWeaver 7.3 EHP 1(经过 JEE5 认证),并且我已经为现有 WSDL 生成了 Web 服务框架。此 Web 服务中的所有方法都是非阻塞的,因此用 @Onewa
我是一名优秀的程序员,十分优秀!