[英]JAX-WS - inject WebServiceContext into @WebService that only has @Oneway methods
[英]JAX-WS, WebServiceContext and Session
我可以在Web服務中使用WebServiceContext和方法getMessageContext()獲取用於保存的HttpSession對象並獲取會話值嗎? 我試圖在這樣的Web服務中使用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批注嗎? 如果我不使用此注釋會怎樣?
您是否看到了參考實現 zip發行版中包含的stateful
示例?
該示例在Web服務實現中使用注釋com.sun.xml.ws.developer.Stateful
和類com.sun.xml.ws.developer.StatefulWebServiceManager
來存儲對象。 由於要求Java Web服務必須是無狀態的,因此我們需要在客戶端調用之間將對象的內容保存在持久性存儲中。
換句話說,您應該更喜歡穩定的服務。 失敗處理,與事務語義的交互很簡單。 並且增強了可重用性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.