You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by erj2code <pe...@tetraconcepts.com> on 2013/10/11 20:19:47 UTC
How do I convert the map returned to Camel into JSON?
This seems like such a simple thing to do, yet I can't seem to find it in the
Camel documentation.
Related to my last question i.e.,
http://camel.465427.n5.nabble.com/Get-BeanCreationException-when-try-to-add-Jackson-Library-to-my-applicationContext-xml-td5741314.html
I now have my code putting the JMS logging response messages into JSON
format.
Here's what I currently get when I run my 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]
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:
{"fruit1DataType":"String","fruit1":"apple","fruit1Calories":"95","fruit1ColorDataType":"String","fruit1CaloriesDataType":"int","fruit1Color":"red"},
Out: null]
How do I modify my applicationContext.xml to have Camel do this?
I think I need a Camel JSON endpoint in route to serialize my map to JSON.
I found this JSON FAQ:
http://camel.apache.org/how-do-i-configure-endpoints.html but its not clear
to me how I would adapt this to serialize my map to JSON.
Here's my current applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<bean
class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"
/>
<context:component-scan base-package="sample" />
<context:annotation-config />
<camel:camelContext id="HelloWorldContext">
<camel:dataFormats>
<camel:json id="jack" library="Jackson"/>
</camel:dataFormats>
<camel:route>
<camel:from
uri="timer://hello.world.request.timer?fixedRate=true&period=10000" />
<camel:to
uri="log:hello.world.request?level=INFO?showAll=true" />
<camel:bean ref="helloWorld" />
<camel:marshal ref ="jack"/>
<camel:to
uri="log:hello.world.response?level=INFO?showAll=true" />
</camel:route>
</camel:camelContext>
<bean id="jms"
class="org.apache.activemq.camel.component.ActiveMQComponent">
<property name="configuration" ref="jmsConfig" />
</bean>
<bean id="jmsConfig"
class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="transacted" value="false" />
<property name="concurrentConsumers" value="1" />
</bean>
<bean id="jmsConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="vm://localhost" />
<property name="redeliveryPolicy" ref="redeliveryPolicy" />
<property name="prefetchPolicy" ref="prefetchPolicy" />
</bean>
<bean id="prefetchPolicy"
class="org.apache.activemq.ActiveMQPrefetchPolicy">
<property name="queuePrefetch" value="5" />
</bean>
<bean id="redeliveryPolicy"
class="org.apache.activemq.RedeliveryPolicy">
<property name="maximumRedeliveries" value="1" />
<property name="backOffMultiplier" value="2" />
<property name="initialRedeliveryDelay" value="2000" />
</bean>
</beans>
--
View this message in context: http://camel.465427.n5.nabble.com/How-do-I-convert-the-map-returned-to-Camel-into-JSON-tp5741370.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: How do I convert the map returned to Camel into JSON?
Posted by erj2code <pe...@tetraconcepts.com>.
Actually to clarify I don't need the , Out: null] part.
Just this part would be fine:
{"fruit1DataType":"String","fruit1":"apple","fruit1Calories":"95","fruit1ColorDataType":"String","fruit1CaloriesDataType":"int","fruit1Color":"red"}
--
View this message in context: http://camel.465427.n5.nabble.com/How-do-I-convert-the-map-returned-to-Camel-into-JSON-tp5741370p5741373.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: How do I convert the map returned to Camel into JSON?
Posted by James Carman <ja...@carmanconsulting.com>.
No problem. Happy camel riding!
On Fri, Oct 11, 2013 at 3:38 PM, erj2code <pe...@tetraconcepts.com> wrote:
> Thanks, that worked!
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-do-I-convert-the-map-returned-to-Camel-into-JSON-tp5741370p5741377.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
Re: How do I convert the map returned to Camel into JSON?
Posted by erj2code <pe...@tetraconcepts.com>.
Thanks, that worked!
--
View this message in context: http://camel.465427.n5.nabble.com/How-do-I-convert-the-map-returned-to-Camel-into-JSON-tp5741370p5741377.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: How do I convert the map returned to Camel into JSON?
Posted by James Carman <ja...@carmanconsulting.com>.
Try something like this:
<camel:route>
<camel:from uri="timer://hello.world.request.timer?fixedRate=true&period=10000"
/>
<camel:to uri="log:hello.world.request?level=INFO?showAll=true" />
<camel:bean ref="helloWorld" />
<camel:marshal ref ="jack"/>
<camel:convertBodyTo type="java.lang.String" />
<camel:log message="${body}"/>
<camel:to uri="log:hello.world.response?level=INFO?showAll=true" />
</camel:route>
On Fri, Oct 11, 2013 at 3:22 PM, erj2code <pe...@tetraconcepts.com> wrote:
> Would I use convertBodyTo described on
> http://camel.apache.org/convertbodyto.html to convert the body to String?
>
> I'm still a newbie with Camel; could you show me an example?
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-do-I-convert-the-map-returned-to-Camel-into-JSON-tp5741370p5741375.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
Re: How do I convert the map returned to Camel into JSON?
Posted by erj2code <pe...@tetraconcepts.com>.
Would I use convertBodyTo described on
http://camel.apache.org/convertbodyto.html to convert the body to String?
I'm still a newbie with Camel; could you show me an example?
--
View this message in context: http://camel.465427.n5.nabble.com/How-do-I-convert-the-map-returned-to-Camel-into-JSON-tp5741370p5741375.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: How do I convert the map returned to Camel into JSON?
Posted by James Carman <ja...@carmanconsulting.com>.
Try converting the body to a String first (since it looks like it's a
byte[]), then log just the body.
On Fri, Oct 11, 2013 at 2:19 PM, erj2code <pe...@tetraconcepts.com> wrote:
> This seems like such a simple thing to do, yet I can't seem to find it in the
> Camel documentation.
>
> Related to my last question i.e.,
> http://camel.465427.n5.nabble.com/Get-BeanCreationException-when-try-to-add-Jackson-Library-to-my-applicationContext-xml-td5741314.html
> I now have my code putting the JMS logging response messages into JSON
> format.
>
> Here's what I currently get when I run my 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]
>
>
> 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:
>
>
>
> {"fruit1DataType":"String","fruit1":"apple","fruit1Calories":"95","fruit1ColorDataType":"String","fruit1CaloriesDataType":"int","fruit1Color":"red"},
> Out: null]
>
> How do I modify my applicationContext.xml to have Camel do this?
>
> I think I need a Camel JSON endpoint in route to serialize my map to JSON.
> I found this JSON FAQ:
> http://camel.apache.org/how-do-i-configure-endpoints.html but its not clear
> to me how I would adapt this to serialize my map to JSON.
>
> Here's my current applicationContext.xml:
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <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">
>
> <bean
>
> class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"
> />
> <context:component-scan base-package="sample" />
> <context:annotation-config />
>
> <camel:camelContext id="HelloWorldContext">
>
>
> <camel:dataFormats>
> <camel:json id="jack" library="Jackson"/>
> </camel:dataFormats>
>
> <camel:route>
>
> <camel:from
>
> uri="timer://hello.world.request.timer?fixedRate=true&period=10000" />
> <camel:to
> uri="log:hello.world.request?level=INFO?showAll=true" />
> <camel:bean ref="helloWorld" />
>
>
> <camel:marshal ref ="jack"/>
> <camel:to
> uri="log:hello.world.response?level=INFO?showAll=true" />
> </camel:route>
>
> </camel:camelContext>
>
> <bean id="jms"
> class="org.apache.activemq.camel.component.ActiveMQComponent">
> <property name="configuration" ref="jmsConfig" />
> </bean>
>
> <bean id="jmsConfig"
> class="org.apache.camel.component.jms.JmsConfiguration">
> <property name="connectionFactory" ref="jmsConnectionFactory" />
> <property name="transacted" value="false" />
> <property name="concurrentConsumers" value="1" />
> </bean>
>
> <bean id="jmsConnectionFactory"
> class="org.apache.activemq.ActiveMQConnectionFactory">
> <property name="brokerURL" value="vm://localhost" />
> <property name="redeliveryPolicy" ref="redeliveryPolicy" />
> <property name="prefetchPolicy" ref="prefetchPolicy" />
> </bean>
>
> <bean id="prefetchPolicy"
> class="org.apache.activemq.ActiveMQPrefetchPolicy">
> <property name="queuePrefetch" value="5" />
> </bean>
>
> <bean id="redeliveryPolicy"
> class="org.apache.activemq.RedeliveryPolicy">
> <property name="maximumRedeliveries" value="1" />
> <property name="backOffMultiplier" value="2" />
> <property name="initialRedeliveryDelay" value="2000" />
> </bean>
>
> </beans>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-do-I-convert-the-map-returned-to-Camel-into-JSON-tp5741370.html
> Sent from the Camel - Users mailing list archive at Nabble.com.