尝试添加杰克逊库时获取BeanCreationException

时间:2013-10-10 20:57:40

标签: java xml json spring apache-camel

我有一个简单的Hello World示例,它将Map传递给Camel并通过Log4J将值显示到控制台。我想扩展此示例,通过将Jackson库添加到我的Camel applicationContext.xml

来在JSON中呈现此地图

首先,我尝试将以下XML标记添加到我的applicationContext.xml中(如下所示) “在Spring DSL中使用JSON”下的http://camel.apache.org/json.html

<camel:dataFormats>
    <camel:json id="jack" library="Jackson"/>
</camel:dataFormats>

但是当我将它添加到我的applicationContext.xml并运行我的Java代码时,我得到以下XmlBeanDefinitionStoreException消息:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'dataFormats'.     One of '{"http://camel.apache.org/schema/
 spring":route}' is expected. 

在camelContext内部或外部移动这些标记会产生相同的错误(在camelContext内部只有一个较长的URL列表)。

我需要在ApplicationContext.xml中指定其他内容吗?

这是我当前的applicationContext.xml:

更新:以下xml现在可以使用了。不得不移动dataFormats 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&amp;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>

1 个答案:

答案 0 :(得分:2)

The dateFormats and json elements are part of the camel namespace.您需要指定

<camel:dataFormats>
    <camel:json id="jack" library="Jackson"/>
</camel:dataFormats>