问题描述
我有一个简单的 Hello World 示例,它将 Map 传递给 Camel,并通过 Log4J 将值显示到控制台.我想通过将 Jackson 库添加到我的 Camel applicationContext.xml 来扩展此示例以在 JSON 中呈现此地图
I have a simple Hello World example that passes a Map to Camel and displays the values to the console via Log4J. I want to expand this example to render this map in JSON by adding the Jackson library to my Camel applicationContext.xml
首先我尝试将以下 XML 标记添加到我的 applicationContext.xml(如在http://www.51sjk.com/Upload/Articles/1/0/337/337173_20221117112439033.html 在在 Spring DSL 中使用 JSON"下)
First I tried adding the following XML tags to my applicationContext.xml (as specified at http://www.51sjk.com/Upload/Articles/1/0/337/337173_20221117112439033.html under "Using JSON in Spring DSL")
<camel:dataFormats> <camel:json id="jack" library="Jackson"/> </camel:dataFormats>
但是当我将它添加到我的 applicationContext.xml 并运行我的 Java 代码时,我收到以下 XmlBeanDefinitionStoreException 消息:
But when I add this to my applicationContext.xml, and run my Java code I get the following XmlBeanDefinitionStoreException message:
cvc-complex-type.2.4.a: Invalid content was found starting with element 'dataFormats'. One of '{"http://www.51sjk.com/Upload/Articles/1/0/337/337173_20221117112443674.jpg spring":route}' is expected.
在我的 camelContext 内部或外部移动这些标签会产生相同的错误(在 camelContext 内部时只是一个更长的 URL 列表).
Moving these tags inside or outside of my camelContext yields the same error (just a longer list of URLs when inside the camelContext).
我还需要在我的 ApplicationContext.xml 中指定其他内容吗?
Is there something else I need to specify in my ApplicationContext.xml?
这是我当前的 applicationContext.xml:
Here is my current applicationContext.xml:
更新:以下 xml 现在可以使用.不得不移动 dataFormats XML 标记的位置.
UPDATED: The following xml now works. Had to move the location of the dataFormats XML tags.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.51sjk.com/Upload/Articles/1/0/337/337173_20221117112444142.jpg" xmlns:xsi="http://www.51sjk.com/Upload/Articles/1/0/337/337173_20221117112444893.jpg" xmlns:camel="http://www.51sjk.com/Upload/Articles/1/0/337/337173_20221117112443674.jpgspring" xmlns:context="http://www.51sjk.com/Upload/Articles/1/0/337/337173_20221117112450814.jpg" xmlns:util="http://www.51sjk.com/Upload/Articles/1/0/337/337173_20221117112451389.jpg" xsi:schemaLocation=" http://www.51sjk.com/Upload/Articles/1/0/337/337173_20221117112444142.jpg http://www.51sjk.com/Upload/Articles/1/0/337/337173_20221117112444142.jpg/spring-beans-3.0.xsd http://www.51sjk.com/Upload/Articles/1/0/337/337173_20221117112451389.jpg http://www.51sjk.com/Upload/Articles/1/0/337/337173_20221117112451389.jpg/spring-util-3.0.xsd http://www.51sjk.com/Upload/Articles/1/0/337/337173_20221117112443674.jpgspring http://www.51sjk.com/Upload/Articles/1/0/337/337173_20221117112443674.jpgspring/camel-spring.xsd http://www.51sjk.com/Upload/Articles/1/0/337/337173_20221117112450814.jpg http://www.51sjk.com/Upload/Articles/1/0/337/337173_20221117112450814.jpg/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: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" /> <property name="useExponentialBackOff" value="true" /> </bean> </beans>
推荐答案
dateFormats 和 json 元素是 camel 命名空间的一部分.您需要指定
The dateFormats and json elements are part of the camel namespace. You need to specify that
<camel:dataFormats> <camel:json id="jack" library="Jackson"/> </camel:dataFormats>