Apache camel Body不能转换为DBObject类型,也不能转换为List <dbobject>

时间:2016-02-25 08:17:34

标签: json mongodb apache-camel

我正在使用Apache Camel为MongoDB尝试一个示例。

我做的是这样的: 我有我的主要方法:

public static void main(String[] args) {
         ApplicationContext context = new ClassPathXmlApplicationContext("camel-context-mongo.xml");
     }

我的camel-context-mongo.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" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<camel:camelContext id="camel-client">
<camel:routeBuilder ref="vinodroute" />
</camel:camelContext>

<bean id="myDb" class="com.mongodb.Mongo">
<constructor-arg index="0" value="localhost" />
</bean>
<bean id="vinodroute" class="com.camel.test.CamelMongoRoute" />
</beans>

我的骆驼路线是

public void configure() throws Exception {
        from("jetty:http://localhost:8181/data")
        .to("mongodb:myDb?database=DataDB&collection=data&operation=findAll");
        from("jetty:http://localhost:8181/dataInsert")
        .to("mongodb:myDb?database=DataDB&collection=data&operation=insert");
                }

现在我正在尝试插入操作,即我试图通过soapUI调用http://localhost:8181/dataInsert休息服务,我的json有效负载是:

[{
    "DataID": "19dd8a2e-45cb-40cc-ba2c-15d5a81733f8",
    "DataDescription": "Data High",
    "DataPriority": "High"
  },
  {
    "DataID": "cfc6f177-fe18-419e-84cd-db11617f12af",
    "DataDescription": "Data Low Low",
    "DataPriority": "Low"
  },
  {
    "DataID": "822accd4-061f-491b-a417-1c683d0c268f",
    "DataDescription": "Data High",
    "DataPriority": "High"
  },
  {
    "DataID": "1c7dcf89-629c-4523-96c4-a2cc740d1769",
    "DataDescription": "Data High",
    "DataPriority": "High"
  },
  {
    "DataID": "1c0b5e66-3ca4-4a0a-a9a5-8d1397589ee9",
    "DataDescription": "Data Low",
    "DataPriority": "Low"
  },
  {
    "DataID": "05677b88-d43b-4224-be7e-66360a2f35a8",
    "DataDescription": "Data High",
    "DataPriority": "High"
  }]

现在上面的json是一个有效的json。但我仍然得到这个例外:

<data contentType="text/plain;charset=ISO-8859-1" contentLength="3128"><![CDATA[org.apache.camel.component.mongodb.CamelMongoDbException: MongoDB operation = insert, Body is not conversible to type DBObject nor List<DBObject>
    at org.apache.camel.component.mongodb.MongoDbProducer.doInsert(MongoDbProducer.java:261)
    at org.apache.camel.component.mongodb.MongoDbProducer.invokeOperation(MongoDbProducer.java:106)
    at org.apache.camel.component.mongodb.MongoDbProducer.process(MongoDbProducer.java:72)
    at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
    at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:110)
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:72)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:398)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:191)
    at org.apache.camel.component.jetty.CamelContinuationServlet.service(CamelContinuationServlet.java:151)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:652)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1320)
    at org.eclipse.jetty.servlets.MultiPartFilter.doFilter(MultiPartFilter.java:119)
    at org.apache.camel.component.jetty.CamelFilterWrapper.doFilter(CamelFilterWrapper.java:44)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1291)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:443)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1044)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:372)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:978)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
    at org.eclipse.jetty.server.Server.handle(Server.java:367)
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:486)
    at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:937)
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:998)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:861)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
    at java.lang.Thread.run(Thread.java:722)
]]></data>

我做错了什么?期待您的解决方案。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

尝试使用jackon将您的消息解组到列表中。 MongoDB组件知道如何创建DBObject,但不知道如何创建DBObject,您必须首先提供可转换为List的内容。

from("jetty:http://localhost:8181/data")
  .unmarshall(new ListJacksonDataFormat())
  .to("mongodb:myDb?database=DataDB&collection=data&operation=findAll");

答案 1 :(得分:0)

from("")
    .marshal().json(JsonLibrary.Jackson)
    .unmarshal(new ListJacksonDataFormat())
    .to("mongodb:mongoClient?database=mytest&collection=testc&operation=insert")

这对我有用