2013-07-03 29 views
1

我有一個http-inbound-gateway,我想要檢索一個請求參數來填充入站通道作爲有效負載。我發現http-inbound-gateway支持spring表達式,所以我可以使用#requestParams來檢索請求參數。看起來#requestParams相當於request.getParameters('key'),它返回一個Map,但我希望調用request.getParameter('key'),它返回一個String。目前我必須使用新的字符串(#requestParams ['key'])來解決這個問題。有沒有更好的方法來做到這一點?如何在Spring集成http-inbound-gateway中通過spring el檢索請求參數?

<int-http:inbound-gateway path="/mypath" 
    supported-methods="GET" payload-expression="new String(#requestParams['key']?:'')" 
    request-channel="inboundChannel" reply-channel="outboundChannel" 
    error-channel="errorChannel" 
    message-converters="stringHttpMessageConverterUsingUtf8">  
</int-http:inbound-gateway> 

回答

1

我找到源代碼中org.springframework.integration.http.inbound.HttpRequestHandlingEndpointSupport

@SuppressWarnings({ "rawtypes", "unchecked" }) 
private Message<?> actualDoHandleRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws IOException { 
//omitted codes 
LinkedMultiValueMap<String, String> requestParams = this.convertParameterMap(servletRequest.getParameterMap()); 
     evaluationContext.setVariable("requestParams", requestParams); 
//omitted codes  
} 

所以#requestParameters是LinkedMultiValueMap對象。我可以使用

#requestParams.get('your key')?:''//null safe 

獲得所需的字符串請求參數。

1

requestParams變量是LinkedMultiValueMap類型的對象。 LinkedMultiValueMap#get方法返回List。所以你的代碼#requestParams.get('your key')?:''//null safe返回字符串列表。它在大多數情況下都能正常工作,但比如(#requestParams.get('your key')?:'').equals('value')會失敗。爲獲得第一個字符串參數全空安全的代碼是:

(#requestParams.get('your key')?:{''})[0] 

注意LinkedMultiValueMap是在LinkedList存儲多個值。如果您只需要一個請求參數值,則可以考慮使用LinkedMultiValueMap#toSingleValueMap方法。

+0

謝謝,很好的建議。 – Hippoom

相關問題