Question

I have a http-inbound-gateway and I want to retrieve a request parameter to fill in the inbound channel as payload. I find that http-inbound-gateway supports spring expression, so I can use #requestParams to retrieve request parameters. It seems #requestParams is equivalent to request.getParameters('key') which returns a Map but I expect to invoke request.getParameter('key') which returns a String. Currently I have to use new String(#requestParams['key']) to fix this. Is there any better way to do this?

<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>
Was it helpful?

Solution

I find the source code in 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    
}

so the #requestParameters is a LinkedMultiValueMap object. I can use

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

to get the desired String request parameter.

OTHER TIPS

The requestParams variable is an object of LinkedMultiValueMap type. LinkedMultiValueMap#get method returns List. So your code #requestParams.get('your key')?:''//null safe returns list of Strings. It will work fine in most cases, but for example the comparison (#requestParams.get('your key')?:'').equals('value') will fail. The full null safe code for getting first string parameter is:

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

Note that LinkedMultiValueMap is storing multiple values in a LinkedList. If you need only one request parameter value you can consider using LinkedMultiValueMap#toSingleValueMap method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top