Search code examples
springhttpintegrationspring-elpayload

How to get payload values from byte[] message in Spring Integration int-http:outbound-gateway?


I am configuring an outbound-gateway in my context.xml where I want to extract values from a byte[] type payload and use them to construct a URI. I found that SpEL allows you to build it this way:

url-expression="T(org.springframework.web.util.UriComponentsBuilder)
                       .fromHttpUrl('http://HOST:PORT/PATH')
                       .queryParams(payload)
                       .build()
                       .toUri()"

Source: https://docs.spring.io/spring-integration/reference/html/http.html#mapping-uri-variables

My variation of the solution looks like this:

<int-http:outbound-gateway id="candleRequestGateway"
                           request-channel="candleRequestChannel"
                           reply-channel="dataResponseChannel"
                           http-method="GET"
                           url-expression="T(org.springframework.web.util.UriComponentsBuilder)
                                        .fromHttpUrl('some/{path}')
                                        .queryParam('myParam', payload.get('myParam'))
                                        .buildAndExpand(payload.get('path'))
                                        .toUri()"/>

However, I am getting the following error which occurs while executing payload.get('myParam') part:

org.springframework.messaging.MessageHandlingException: error occurred in message handler [org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler#3]; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method get(java.lang.String) cannot be found on type byte[]

I understand and agree with the error. My question: is there a way (a specific SpEL expression(?)) to extract the values from byte[] payload without having to transform it before it reaches the outbound-gateway? Is this a valid solution at all?


Solution

  • Exactly what are you expecting payload.get('myParam') and payload.get('path') to do when the payload is a byte[].

    Clearly, a byte[] does not have a get(String) method.

    to extract the values from byte[]

    Extract how? A byte[] is an unstructured array of bytes; the only thing you can do is something like new String(payload).substring(0, 5) or similar.

    If the bytes contain JSON, you could use a #jsonPath SpEL function.