【问题标题】:How do I convert the map returned to Camel into JSON?如何将返回给 Camel 的地图转换为 JSON?
【发布时间】:2013-10-11 16:32:09
【问题描述】:

这似乎是一件很简单的事情,但我似乎无法在 Camel 文档中找到它。

与我的上一个问题有关,即Get BeanCreationException when try to add Jackson Library 我现在有我的代码将 JMS 日志记录响应消息转换为 JSON 格式。

这是我当前运行代码时得到的结果:

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]

理想情况下,我只想让 Camel 将返回的地图内容转换为 JSON 并将其打印到控制台,如下所示:

{"fruit1DataType":"String","fruit1":"apple","fruit1Calories":"95","fruit1ColorDataType":"String","fruit1CaloriesDataType":"int","fruit1Color":"red"}, Out: null]

如何修改我的 applicationContext.xml 以让 Camel 执行此操作?

这是我当前的 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">

        <!-- Add Jackson library to render Java Map into JSON -->
        <camel:dataFormats>
          <camel:json id="jack" library="Jackson"/>
        </camel:dataFormats>

        <camel:route>
            <!-- sends a request to the hello world JMS queue every 10 seconds -->
            <camel:from
                uri="timer://hello.world.request.timer?fixedRate=true&amp;period=10000" />
            <camel:to uri="log:hello.world.request?level=INFO?showAll=true" />
            <camel:bean ref="helloWorld" />

            <!-- now print out the map in JSON format -->
            <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>

【问题讨论】:

    标签: java json map jackson apache-camel


    【解决方案1】:

    您需要在路由中使用 Camel JSON endpoint 将您的 Map 序列化为 JSON。该链接还显示了如何选择在端点中使用的序列化程序(我认为 Jackson 是最佳选择)。

    类似这样的东西(不准确)

    <camelContext>
            <dataFormats>
                <json id="serializer" library="Jackson"/>
            </dataFormats>
            <route>
                <from uri="servlet:///test-camel"/>
                <to uri="bean:someSpringBean"/>
                <to uri="bean:anotherSpringBean"/>
                <marshal ref="serializer"/>
            </route>            
    </camelContext>
    

    请注意,json 元素还具有一个名为 unmarshalTypeName 的属性,用于序列化/反序列化到的类型。但是,这默认为Map,如上面提到的链接中所述。由于您有Map,因此您无需指定。

    【讨论】:

    • 我假设你引用了这个例子: 我试过玩这个,但还没有让它工作。猜猜我还不明白语法。你能给我一个与我的 applicationContext.xml 相关的例子吗?
    • 我找到了这个 Camel 常见问题页面:camel.apache.org/how-do-i-configure-endpoints.html“使用 Spring XML”下的部分与我想要的最相关,但是如何使用它来将我的 Map 序列化为 JSON?跨度>
    • Vidya,你能给我提供一个 Camel JSON 端点的简单示例吗?
    • 添加了一个 sn-p,您需要对其进行调整,但希望能提供总体思路。
    【解决方案2】:

    您可以通过简单的方式做到这一点:

    <route>
        <from id="from1" uri="anyway" />
        <marshal id="_marshal1">
            <json library="Jackson"/>
        </marshal>
        <log id="_log1" message="${body}"/>
        <to id="_to1" uri="mock:foo"/>
    </route>
    

    并在.pom中导入依赖

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jackson</artifactId>
        <version>2.9.2</version>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 2017-02-14
      • 2023-03-08
      • 2018-03-03
      • 1970-01-01
      相关资源
      最近更新 更多