Stannis Lin
Stannis Lin

Reputation: 17

Apache Camel "Body is not conversible to type DBObject" error when a message transfered from MQTT to MongoDB endpoint

I created an Apache Camel Route for MQTT message transforming from MQTT broker to MongoDB. And I got a "Body is not conversible to type DBObject" error even if the message is already a JSON string for MongoDB.

Now, I used the DBObject class to solve this problem temporarily. But how to routing a MongoDB JSON string message without DBObject in Apache Camel?

The original routing code:

from("mqtt:foo?subscribeTopicName=bar/")
.to("mongodb:myDb?database=foo&collection=bar&operation=insert");

Current solution by 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");

Error log:

org.apache.camel.component.mongodb.CamelMongoDbException: MongoDB operation = insert, Body is not conversible to type DBObject nor List<DBObject>

Upvotes: 0

Views: 1208

Answers (1)

lahu89
lahu89

Reputation: 363

When the body is a string representation of DBOject than converting during the route should work:

from("mqtt:foo?subscribeTopicName=bar/")
.convertBodyTo(DBObject.class)
.to("mongodb:myDb?database=foo&collection=bar&operation=insert");

Upvotes: 1

Related Questions