Java 从GSON parsor到Camel Spring获取数据
我使用SpringDSL将JSON格式的数据获取到Camel。 我这样写代码Java 从GSON parsor到Camel Spring获取数据,java,spring,gson,apache-camel,Java,Spring,Gson,Apache Camel,我使用SpringDSL将JSON格式的数据获取到Camel。 我这样写代码 <bean id="mqtt" class="org.apache.camel.component.mqtt.MQTTComponent"/> <bean id="gson" class="org.apache.camel.component.gson.GsonDataFormat"/> <camel:camelContext xmlns="http://camel.apa
<bean id="mqtt" class="org.apache.camel.component.mqtt.MQTTComponent"/>
<bean id="gson" class="org.apache.camel.component.gson.GsonDataFormat"/>
<camel:camelContext xmlns="http://camel.apache.org/schema/spring">
<camel:route>
<camel:from uri="mqtt:bar?host=tcp://10.30.11.0:1883&subscribeTopicName=apss/messages" />
<unmarshal ref="gson" />
<camel:choice>
<camel:when>
<!-- I dont knwo what to write here -->
<camel:to uri="stream:out" />
</camel:when>
<camel:otherwise>
<camel:to uri="stream:out" />
</camel:otherwise>
</camel:choice>
</camel:route>
</camel:camelContext>
我想将第一个字段与一个字符串进行比较,并决定在解析后执行什么操作。
我知道GSON将把JSON字符串解析为hashmap。所以我想用hashmap做一个get(0)。
但我不知道春天该怎么做。有人能帮我吗?我终于得到了答案。
我把它给别人看
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="mqtt:apsssub?host=tcp://10.30.11.0:1883&subscribeTopicName=apss/messages" />
<unmarshal ref="gson" />
<bean beanType="com.hk.MessageHeaderSetter" method="putMessageTypeInHeader" />
<choice>
<when>
<simple>${headers.msg_type} == 'location_update'</simple>
<log message="Message type 1." />
<to uri="jms:LOCATION_UPDATE_QUEUE" />method="updateLocation" />
</when>
<otherwise>
<log message="Other message" />
<log message="Headers ${headers}" />
<to uri="stream:out" />
</otherwise>
</choice>
</route>
</camelContext>
我不知道为什么人们不回答我的问题
public void putMessageTypeInHeader(Exchange exchange) {
exchange.getIn().setHeader("msg_type", ((HashMap)exchange.getIn().getBody()).get("msg_type"));
}