Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I convert the map returned to Camel into JSON?
    primarykey
    data
    text
    <p>This seems like such a simple thing to do, yet I can't seem to find it in the Camel documentation.</p> <p>Related to my last question i.e., <a href="https://stackoverflow.com/questions/19306106/get-beancreationexception-when-try-to-add-jackson-library">Get BeanCreationException when try to add Jackson Library</a> I now have my code putting the JMS logging response messages into JSON format.</p> <p>Here's what I currently get when I run my code:</p> <pre><code>23118 [hello.world.request.timer] INFO hello.world.request - Exchange[Id:e93861e4- a5be-4d63-b658-5939f414e595, ExchangePattern:InOnly, Properties: {CamelToEndpoint=log://hello.world.request?showAll=true, CamelTimerFiredTime=Fri Oct 11 12:03:20 EDT 2013, CamelTimerPeriod=10000, CamelTimerName=hello.world.request.timer}, Headers:{firedTime=Fri Oct 11 12:03:20 EDT 2013}, BodyType:null, Body:null, Out: null] Returning Map key= fruit1DataType, value= String key= fruit1, value= apple key= fruit1Calories, value= 95 key= fruit1ColorDataType, value= String key= fruit1CaloriesDataType, value= int key= fruit1Color, value= red 23122 [hello.world.request.timer] INFO hello.world.response - Exchange[Id:e93861e4- a5be-4d63-b658-5939f414e595, ExchangePattern:InOnly, Properties: {CamelToEndpoint=log://hello.world.response?showAll=true, CamelTimerFiredTime=Fri Oct 11 12:03:20 EDT 2013, CamelTimerPeriod=10000, CamelTimerName=hello.world.request.timer}, Headers:{firedTime=Fri Oct 11 12:03:20 EDT 2013}, BodyType:byte[], Body: {"fruit1DataType":"String","fruit1":"apple","fruit1Calories":"95","fruit1ColorDataType":"St ring","fruit1CaloriesDataType":"int","fruit1Color":"red"}, Out: null] </code></pre> <p>Ideally I'd just like to have Camel convert the contents of the map returned to JSON and print it to the console like this:</p> <pre><code>{"fruit1DataType":"String","fruit1":"apple","fruit1Calories":"95","fruit1ColorDataType":"String","fruit1CaloriesDataType":"int","fruit1Color":"red"}, Out: null] </code></pre> <p>How do I modify my applicationContext.xml to have Camel do this?</p> <p>Here's my current applicationContext.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt; &lt;bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /&gt; &lt;context:component-scan base-package="sample" /&gt; &lt;context:annotation-config /&gt; &lt;camel:camelContext id="HelloWorldContext"&gt; &lt;!-- Add Jackson library to render Java Map into JSON --&gt; &lt;camel:dataFormats&gt; &lt;camel:json id="jack" library="Jackson"/&gt; &lt;/camel:dataFormats&gt; &lt;camel:route&gt; &lt;!-- sends a request to the hello world JMS queue every 10 seconds --&gt; &lt;camel:from uri="timer://hello.world.request.timer?fixedRate=true&amp;amp;period=10000" /&gt; &lt;camel:to uri="log:hello.world.request?level=INFO?showAll=true" /&gt; &lt;camel:bean ref="helloWorld" /&gt; &lt;!-- now print out the map in JSON format --&gt; &lt;camel:marshal ref ="jack"/&gt; &lt;camel:to uri="log:hello.world.response?level=INFO?showAll=true" /&gt; &lt;/camel:route&gt; &lt;/camel:camelContext&gt; &lt;bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent"&gt; &lt;property name="configuration" ref="jmsConfig" /&gt; &lt;/bean&gt; &lt;bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration"&gt; &lt;property name="connectionFactory" ref="jmsConnectionFactory" /&gt; &lt;property name="transacted" value="false" /&gt; &lt;property name="concurrentConsumers" value="1" /&gt; &lt;/bean&gt; &lt;bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"&gt; &lt;property name="brokerURL" value="vm://localhost" /&gt; &lt;property name="redeliveryPolicy" ref="redeliveryPolicy" /&gt; &lt;property name="prefetchPolicy" ref="prefetchPolicy" /&gt; &lt;/bean&gt; &lt;bean id="prefetchPolicy" class="org.apache.activemq.ActiveMQPrefetchPolicy"&gt; &lt;property name="queuePrefetch" value="5" /&gt; &lt;/bean&gt; &lt;bean id="redeliveryPolicy" class="org.apache.activemq.RedeliveryPolicy"&gt; &lt;property name="maximumRedeliveries" value="1" /&gt; &lt;property name="backOffMultiplier" value="2" /&gt; &lt;property name="initialRedeliveryDelay" value="2000" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload