Open rsvalerio opened 10 years ago
Hi @rsvalerio!
BasicDBObject implements Map<String, Object>: http://api.mongodb.org/java/current/com/mongodb/BasicDBObject.html.
So this should already be possible.
What exception are you getting?
Regards, Raúl.
Hi @raulk .
Well, my problem is:
Route:
from("direct:get.beers")
.to("mongodb:local?database=poc&collection=beers&operation=findAll")
.split().body()
.convertBodyTo(String.class)
.unmarshal().xmljson()
.log(LoggingLevel.DEBUG, "### ${body}")
.end();
Throws:
nu.xom.IllegalNameException: NCNames cannot start with the character 24
Because:
{
"_id": {
"$oid": "538b8b63d69eb687a19caef7"
},
"type": "strong"
}
But even toMap() have an invalid json response:
public void toMap (
Exchange exchange,
@Body Map<String, Object> bodyImplementsMap ) {
Map<String, Object> bodyToMap = ((BasicDBObject)bodyImplementsMap).toMap();
exchange.getIn().setHeader("bodyImplementsMap", bodyImplementsMap);
exchange.getIn().setHeader("bodyToMap", bodyToMap);
}
bodyImplementsMap (valid json, invalid xml because $) :
{
"_id": {
"$oid": "538b8b63d69eb687a19caef7"
},
"type": "strong"
}
bodyToMap (invalid json, miss "", valid xml ) :
{
_id = 538b8b63d69eb687a19caef7,
type = strong
}
By now, converting response body from mongodb to Map as follows:
public void toMap ( Exchange exchange, @Body BasicDBObject body) {
ObjectId id = body.getObjectId("_id");
body.put("_id", id.toString());
exchange.getIn().setBody(body.clone());
}
This type converter needs to return a "real" java.util.Map?
From:
To:
This way, wee can convert mongodb response body, directly to xml, o send generated Map directly to a relational database using spring simple jdbc insert, etc...
More, we can use simple language like: ${body[_id]}
What do you think @raulk ?
Or maybe i'm doing things the wrong way :(
Thanks.