How can I access to a property in a Hashmap in Apache Camel 2.14.1?

I'm pretty new working in Camel, I have the next doubt, how can I access to a property in a HashMap in Apache Camel 2.14.1?

I have the next declared bean

    <bean id="mapRegion" class="java.util.HashMap">
        <constructor-arg>
            <map key-type="java.lang.String" value-type="java.lang.String">
                <entry key="R01" value="one" />
                <entry key="R02" value="two" />
            </map>
        </constructor-arg>
    </bean>

If I print the map with camel, I can see the values

<log message="*** MAP = ${ref:mapRegion}" loggingLevel="DEBUG" />
*** MAP = {R02=two, R01=one}

How can I access to an especific property in the map? I have tried with

<log message="*** MAP = ${ref:mapRegion.R01}" loggingLevel="DEBUG" />
<log message="*** MAP = ${ref:mapRegion[R01]}" loggingLevel="DEBUG" />
<log message="*** MAP = ${ref:mapRegion['R01']}" loggingLevel="DEBUG" />

But non of this are working, always is printing empty value.

Thanks in advance

Upvotes: 0

Views: 3443

Answers (2)

The solution that I found to this is the next

        <setProperty propertyName="SERVICE_REGION">
            <simple>${bean:mapRegion?method=get(${property.phoneRegion})}</simple>
        </setProperty>

Using the bean and the id of the bean, we can invoke a specific method of the HashMap, and we can send the key that we are trying to retrieve

Upvotes: 1

Claus Ibsen
Claus Ibsen

Reputation: 55750

That is not supported by the ref function in the simple language to refer to a bean and do OGNL or map lookups.

And btw what is your use-case for this. You just seem to log it at DEBUG level. But what is the business use-case/value of doing that.

Upvotes: 0

Related Questions