arunky
arunky

Reputation: 126

How to get header value in bean method

I have custom bean which i use it in spring integration flow. It has method which takes hashmap as argument and its values are set dynamically. I am trying to set the value from the payload header field(iploc) but i am not able to achieve so. I tried some combination of spel but it not work. Any pointers?

<int:transformer id="ws.transformer"
    input-channel="ws.transformer.in" output-channel="ws.transformer.out">
    <bean class="com.my.Mybean">
        <property name="map">
            <map>
                <entry key="user">
                    <value>"admin"</value>
                </entry>
                <entry key="location">
                    <value>"headers['iploc']"</value>
                </entry>            
            </map>
        </property>
    </bean>
</int:transformer> 

I can alternatively set the value in Service activator, but i am trying if i achieve this in the SI config itself.

Upvotes: 3

Views: 4155

Answers (2)

omnomnom
omnomnom

Reputation: 9139

Spring Integration Transformer can consume entire Message (payload and headers) so there is no point in passing header value by property since transformer already has access to all message headers.

Your transformer bean definition should contain only these properties that do not come from the Message being transformed:

<int:transformer id="ws.transformer" input-channel="ws.transformer.in" output-channel="ws.transformer.out">
  <bean class="com.my.MyTransformer">
    <property name="user" value="admin"/>                    
  </bean>
</int:transformer> 

And your transformer method:

@Transformer
OutgoingPayload transform(IncomingPayload payload, @Header("iploc") String iplocHeader) {
    return doTransform(...);
}

or just consume entire message:

Message<OutgoingPayload> transform(Message<IncomingPayload> message) {
  final String ipLocHeaderValue = message.getHeaders.get("iploc", String.class);
  return doTransform(...);
}

Upvotes: 5

mfisher
mfisher

Reputation: 506

The bean defined within a transformer like that will be instantiated at startup time, not each time a message is received. The normal way to handle a requirement like this is to have a stateless bean with a method that accepts the header value upon each invocation.

Upvotes: 1

Related Questions