【问题标题】:JAX-WS, WebServiceContext and SessionJAX-WS、WebServiceContext 和会话
【发布时间】:2013-11-15 03:46:40
【问题描述】:

我可以在 Web 服务中使用 WebServiceContext 和方法 getMessageContext() 来获取 HttpSession 对象以进行保存并获取会话值吗?我试图在这样的网络服务中使用 HttpSession:

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public class DummyWs {

    @Resource
    private WebServiceContext wsContext;

    @WebMethod(operationName = "sayHello")
    public String sayHello(@WebParam(name = "name") String name) {
        return "hello " + name;
    }

    @WebMethod(operationName="setValue")
    public void setValue(@WebParam(name = "value") String newValue) {
        MessageContext mc = wsContext.getMessageContext();
        HttpSession session = ((javax.servlet.http.HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();
        session.setAttribute("value", newValue);
    }

    @WebMethod(operationName="getValue")
    public String getValue() {
        MessageContext mc = wsContext.getMessageContext();
        HttpSession session = ((javax.servlet.http.HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST)).getSession();
        return (String)session.getValue("value");
    }

}

我看到了其他使用@Stateful 注解的例子,但我没有使用这个。有必要使用@Stateful 注解吗?如果我不使用这个注解会发生什么?

【问题讨论】:

  • 不确定@Stateful 注解是否真的在做任何事情以使底层传输将会话信息添加到 HTTP 会话,但始终建议使用注解。另一方面,你真的需要使用有状态的网络服务吗?它通常总是建议尽可能使用无状态 Web 服务。你可能也想看看这个:stackoverflow.com/questions/988819/stateful-webservice

标签: java web-services


【解决方案1】:

您是否看到Reference Implementation zip 分发包中包含的示例stateful

该示例在 Web 服务实现中使用注释 com.sun.xml.ws.developer.Stateful 和用于存储对象的类 com.sun.xml.ws.developer.StatefulWebServiceManager。由于 Java Web 服务需要是无状态的,因此我们需要 跨客户端调用将对象的内容保存在持久存储中。

另一方面,您应该更喜欢过时的服务。故障处理,与事务语义的交互很简单。并且提高了复用性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 2012-02-09
    • 2011-06-29
    • 2011-12-16
    • 1970-01-01
    • 2013-03-15
    相关资源
    最近更新 更多