2013-01-03 35 views
1

我將一個傳統的Web服務從運行在JBoss中移植到Jetty中與Spring一起運行。如何在Spring JAX-WS Web服務中與HTTP會話進行交互

這裏是我的Web服務類:

@WebService(serviceName = "Authentication") 
public class AuthenticationWebService 
{ 
    @Resource 
    private WebServiceContext context; 

    public boolean authenticate(@WebParam(name = "clientRef") final String clientRef, @WebParam(name = "username") final String username, @WebParam(name = "password") final String password) 
    { 
     ... DO THE ACTUAL LOGIN - DB CALLS ETC 

     // finally, store the username in the httpsession 
     final HttpServletRequest request = (HttpServletRequest) context.getMessageContext().get(MessageContext.SERVLET_REQUEST); 
     final HttpSession session = request.getSession(); 
     session.setAttribute("FIELD_USERNAME", username); 
    } 
} 

要公開服務,我的Spring XML看起來是這樣的:

<bean id="wsExporter" class="org.springframework.remoting.jaxws.SimpleHttpServerJaxWsServiceExporter"> 
     <property name="basePath" value="/webServices/"/> 
     <property name="port" value="9494"/> 
    </bean> 

    <bean class="com.example.AuthenticationWebService" /> 

Web服務是我的碼頭服務器導出的,我可以把它(通過使用JaxWsPortProxyFactoryBean的Junit類),但是通過request.getSession()獲得了nullpointerexception。

看起來好像「javax.xml.ws.servlet.request」不是context.getMessageContext()映射中的條目。

已經搜索了所有的建議,指出這是獲得servlet請求的正確方法。這可能是我用Spring出口服務的方式嗎?

注 - 我不能也不想改變這個遺留服務在設置/讀取http會話中的值時的行爲,但我會對web服務可以做到這一點的其他方式感興趣。

回答

0

默認情況下會話不是由jaxws維護的。在獲得會話之前添加此行。

context.getMessageContext().put(javax.xml.ws.BindingProvider.SESSION_MAINTAIN_PROPERTY, true) 
+0

爲什麼要這樣做?提供了WebServiceContext,但context.getMessageContext()。get(MessageContext.SERVLET_REQUEST)返回null。 – shuttsy

+0

更新了我的答案。 –

+0

我試過了,但它沒有區別:context.getMessageContext()。get(「javax.xml.ws.servlet.request」)仍然返回null,因此我無法從中獲取http會話。 – shuttsy

相關問題