java - 尝试添加 Jackson 库时出现 BeanCreationException

标签 java xml json spring apache-camel

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

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

<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>

最佳答案

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

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

关于java - 尝试添加 Jackson 库时出现 BeanCreationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19306106/

相关文章:

Java - 即时改变倒数计时器的速度

java - 如何处理编译器中的字符串文字错误

java - 如何评价记录器的性能?

c# - Linq to XML - 将节点解析为字典

xml - 如何根据属性值选择 XmlElement?

android - ListView 中的 TextField 可聚焦性

c# - JsonConvert.DeserializeObject 的 JSON.net 问题

java - spring-如果使用注解需要在xml中指定bean吗

javascript - 从 json 对象获取属性键

java - 需要建议交换复杂 json 的键和值