我想使用 Wildfly 10.0 构建 Jax-ws Web 服务,并且我想要这个具有状态会话的 Web 服务(在 session 上读取和写入),我已经搜索过了,我看到了以下链接: https://docs.oracle。 com/cd/E14571_01/web.1111/e13734/stateful.htm#WSADV234
不幸的是,会话代码不起作用当我调用使用会话的 sayHello 方法时,时间完成并返回(异常:java.lang.NullPointerException 消息:java.lang.NullPointerException)。
我正在使用 Eclipse Mars、动态 Web 服务、Wildfly 10.0 和 Web 服务(服务器:使用 Wildfly 10.0,Web 服务运行时:Apache Axis,并为此项目使用 ear 文件)
代码是:
package com.sample;
@WebService(targetNamespace = "http://sample.com/", serviceName = "Test1Service", portName = "Test1Port")
@Stateful
public class Test1 {
@Resource
private WebServiceContext wsContext;
@WebMethod
public String sayHello() {
MessageContext mc = wsContext.getMessageContext();
HttpSession session = ((javax.servlet.http.HttpServletRequest) mc.get(MessageContext.SERVLET_REQUEST))
.getSession();
if (session == null)
throw new WebServiceException("No HTTP Session found");
String item = "";
try {
item = (String) session.getAttribute("name1");
} catch (Exception e) {
e.printStackTrace();
}
if (item == null || item.equals(""))
item = "good";
session.setAttribute("name1", item);
return "Hello " + session.getAttribute("name1");
}
@WebMethod
public int setValue(int x) {
return x;
}
}
请注意 setValue 方法成功,但 sayHello 方法没有工作