当消息从 MQTT 传输到 MongoDB 端点时,Apache Camel "Body is not conversible to type DBObject" 出错
Apache Camel "Body is not conversible to type DBObject" error when a message transfered from MQTT to MongoDB endpoint
我为从 MQTT 代理转换为 MongoDB 的 MQTT 消息创建了一个 Apache Camel Route。即使消息已经是 MongoDB.
的 JSON 字符串,我也会收到 "Body is not conversible to type DBObject" 错误
现在,我使用DBObjectclass暂时解决了这个问题。但是如何在 Apache Camel 中路由没有 DBObject 的 MongoDB JSON 字符串消息?
原路由代码:
from("mqtt:foo?subscribeTopicName=bar/")
.to("mongodb:myDb?database=foo&collection=bar&operation=insert");
DBObject 的当前解决方案class:
from("mqtt:foo?subscribeTopicName=bar/").process(
new Processor(){
@Override
public void process(Exchange exchange) throws Exception {
String payload = exchange.getIn().getBody(String.class);
DBObject doc=new BasicDBObject();
doc.put("message", payload);
exchange.getIn().setBody(doc, DBObject.class);
}
}
)
.to("mongodb:myDb?database=foo&collection=bar&operation=insert");
错误日志:
org.apache.camel.component.mongodb.CamelMongoDbException: MongoDB operation = insert, Body is not conversible to type DBObject nor List<DBObject>
当正文是 DBOject 的字符串表示时,在路由期间进行转换应该有效:
from("mqtt:foo?subscribeTopicName=bar/")
.convertBodyTo(DBObject.class)
.to("mongodb:myDb?database=foo&collection=bar&operation=insert");
我为从 MQTT 代理转换为 MongoDB 的 MQTT 消息创建了一个 Apache Camel Route。即使消息已经是 MongoDB.
的 JSON 字符串,我也会收到 "Body is not conversible to type DBObject" 错误现在,我使用DBObjectclass暂时解决了这个问题。但是如何在 Apache Camel 中路由没有 DBObject 的 MongoDB JSON 字符串消息?
原路由代码:
from("mqtt:foo?subscribeTopicName=bar/")
.to("mongodb:myDb?database=foo&collection=bar&operation=insert");
DBObject 的当前解决方案class:
from("mqtt:foo?subscribeTopicName=bar/").process(
new Processor(){
@Override
public void process(Exchange exchange) throws Exception {
String payload = exchange.getIn().getBody(String.class);
DBObject doc=new BasicDBObject();
doc.put("message", payload);
exchange.getIn().setBody(doc, DBObject.class);
}
}
)
.to("mongodb:myDb?database=foo&collection=bar&operation=insert");
错误日志:
org.apache.camel.component.mongodb.CamelMongoDbException: MongoDB operation = insert, Body is not conversible to type DBObject nor List<DBObject>
当正文是 DBOject 的字符串表示时,在路由期间进行转换应该有效:
from("mqtt:foo?subscribeTopicName=bar/")
.convertBodyTo(DBObject.class)
.to("mongodb:myDb?database=foo&collection=bar&operation=insert");