camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ron Anderson <biker_...@yahoo.com>
Subject Re: MongoDB Jackson Date Mapping Option
Date Sat, 16 Feb 2013 00:13:44 GMT
Thanks so much for your help - For anyone else trying to solve this I'll show
what I ended up doing that worked to map a Java date to a Bson date or
ISODate in MongoDB.

Created a custom serializer class:

public class BsonDateSerializer extends JsonSerializer<Date>{


@Override
public void serialize(Date value, JsonGenerator jgen, SerializerProvider
provider) throws IOException,     JsonProcessingException {
            jgen.writeStartObject();
            serializeContents(value, jgen, provider);
            jgen.writeEndObject();

    }
	
private void serializeContents(Date value, JsonGenerator jgen,
SerializerProvider provider) throws IOException {
	jgen.writeFieldName("$date");
	SimpleDateFormat formatter = new
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        String formattedDate = formatter.format(value);
        jgen.writeString(formattedDate);
}
}

Annotated my pojo to use custom serializer:

@JsonSerialize(using = BsonDateSerializer.class)
public Date getStartTime() {
	return startTime;
}

And the Mongo route was already working but repeated here:

from("activemq:queue:incomingRecords")
	.routeId("record_processor")
        .marshal(jackson)
	.convertBodyTo(String.class)

.to("mongodb:mongoConn?database=usage&collection=lteusage&operation=insert")
.end();



--
View this message in context: http://camel.465427.n5.nabble.com/MongoDB-Jackson-Date-Mapping-Option-tp5727548p5727671.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Mime
View raw message