2010-10-20 35 views
6

如何在Spring-ws端點中訪問HTTP標頭?如何訪問Spring-ws端點中的HTTP標頭?

我的代碼如下所示:

public class MyEndpoint extends AbstractMarshallingPayloadEndpoint { 
    protected Object invokeInternal(Object arg) throws Exception { 
     MyReq request = (MyReq) arg; 
     // need to access some HTTP headers here 
     return createMyResp(); 
    } 
} 

invokeInternal()僅獲取解組JAXB對象作爲參數。如何訪問invokeInternal()內的請求附帶的HTTP標頭?

可能工作的一種方法是創建一個Servlet過濾器,將標頭值存儲到ThreadLocal變量,然後在invokeInternal()內訪問該變量,但是有沒有更好的,更像彈簧一樣的方法來執行此操作?

回答

12

您可以添加這些方法。 TransportContextHolder將在線程局部變量中保存與傳輸相關的一些數據(本例中爲HTTP)。您可以從TransportContext訪問HttpServletRequest

protected HttpServletRequest getHttpServletRequest() { 
    TransportContext ctx = TransportContextHolder.getTransportContext(); 
    return (null != ctx) ? ((HttpServletConnection) ctx.getConnection()).getHttpServletRequest() : null; 
} 

protected String getHttpHeaderValue(final String headerName) { 
    HttpServletRequest httpServletRequest = getHttpServletRequest(); 
    return (null != httpServletRequest) ? httpServletRequest.getHeader(headerName) : null; 
} 
1

我有同樣的問題(見other question)。我需要添加一個Content-Type頭到我的WS。我走了Servlet過濾器的道路。大多數情況下,您不需要在Web服務中更改HTTP標頭。但是......理論和實踐之間有時會有所不同。

相關問題