0

I have pojo like this:

@Document(collection = "data")
public class DataPoint {
    @Id
    private String id;
    private LocalDateTime createdDate;
    ....
}

in some code base I have following code:

@Autowired
private ProducerTemplate producerTemplate;

...

final List<DataPoint> dataPoints =....
producerTemplate.sendBody("mongodb:mongoBean?database=" + mongoDataConfiguration.getDatabase()
                                    + "&createCollection=true&operation=insert&collection=" + mongoDataConfiguration.getDataPointCollection(), 
        dataPoints);

But when I open collection in database I see date field like this:

"createdDate" : {
                "month" : "NOVEMBER",
                "year" : 2017,
                "dayOfMonth" : 7,
                "dayOfWeek" : "TUESDAY",
                "dayOfYear" : 311,
                "monthValue" : 11,
                "hour" : 17,
                "minute" : 55,
                "nano" : 259000000,
                "second" : 21,
                "chronology" : {
                        "id" : "ISO",
                        "calendarType" : "iso8601"
                }

But I want to store usual ISO date recognizable for mongoDB. It should be something like this:

ISODate("2017-11-06T12:47:51.720")

How did I try to resolve this issue?

I read this topic:

Thus I created a custom serializer:

public class IsoDateSerializer extends JsonSerializer<DateTime> {
    @Override
    public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
           jgen.writeStartObject();
        serializeContents(value.toDate(), 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);
    }
}

and registered it correctly:

@JsonSerialize(using = IsoDateSerializer.class)
public DateTime getCreatedDate() {
    return new DateTime(Date.from(createdDate.toInstant(ZoneOffset.UTC)));
}

But it throws exception:

org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[ID-host-1510066910268-0-3]
        at org.apache.camel.util.ObjectHelper.wrapCamelExecutionException(ObjectHelper.java:1847) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.util.ExchangeHelper.extractResultBody(ExchangeHelper.java:713) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:515) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:511) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:163) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:168) ~[camel-core-2.20.0.jar:2.20.0]
        at com.debeers.mis.upload.route.DummyRoute$1.process(DummyRoute.java:113) ~[classes/:na]
        at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63) [camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548) [camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) [camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201) [camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:452) [camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:219) [camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:183) [camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:174) [camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:101) [camel-core-2.20.0.jar:2.20.0]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_111]
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [na:1.8.0_111]
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_111]
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_111]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_111]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_111]
        at java.lang.Thread.run(Thread.java:745) [na:1.8.0_111]
    Caused by: org.apache.camel.component.mongodb.CamelMongoDbException: java.lang.IllegalArgumentException: Invalid BSON field name $date
        at org.apache.camel.component.mongodb.MongoDbComponent.wrapInCamelMongoDbException(MongoDbComponent.java:61) ~[camel-mongodb-2.20.0.jar:2.20.0]
        at org.apache.camel.component.mongodb.MongoDbProducer.process(MongoDbProducer.java:98) ~[camel-mongodb-2.20.0.jar:2.20.0]
        at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:186) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.processor.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:86) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:541) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.impl.ProducerCache$1.doInProducer(ProducerCache.java:506) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:369) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:506) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:229) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:144) ~[camel-core-2.20.0.jar:2.20.0]
        at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:161) ~[camel-core-2.20.0.jar:2.20.0]
        ... 18 common frames omitted
    Caused by: java.lang.IllegalArgumentException: Invalid BSON field name $date
        at org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:516) ~[bson-3.4.3.jar:na]
        at com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:221) ~[mongodb-driver-3.4.3.jar:na]
        at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:198) ~[mongodb-driver-3.4.3.jar:na]
        at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:130) ~[mongodb-driver-3.4.3.jar:na]
        at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:61) ~[mongodb-driver-3.4.3.jar:na]
        at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63) ~[bson-3.4.3.jar:na]
        at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29) ~[bson-3.4.3.jar:na]
        at com.mongodb.connection.InsertCommandMessage.writeTheWrites(InsertCommandMessage.java:101) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.InsertCommandMessage.writeTheWrites(InsertCommandMessage.java:43) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.BaseWriteCommandMessage.encodeMessageBodyWithMetadata(BaseWriteCommandMessage.java:129) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.RequestMessage.encodeWithMetadata(RequestMessage.java:160) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.WriteCommandProtocol.sendMessage(WriteCommandProtocol.java:220) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:101) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:67) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:37) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:168) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.DefaultServerConnection.insertCommand(DefaultServerConnection.java:118) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.operation.MixedBulkWriteOperation$Run$2.executeWriteCommandProtocol(MixedBulkWriteOperation.java:465) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.operation.MixedBulkWriteOperation$Run$RunExecutor.execute(MixedBulkWriteOperation.java:656) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.operation.MixedBulkWriteOperation$Run.execute(MixedBulkWriteOperation.java:411) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:177) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:168) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:426) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:417) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:168) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:74) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.Mongo.execute(Mongo.java:845) ~[mongodb-driver-3.4.3.jar:na]
        at com.mongodb.Mongo$2.execute(Mongo.java:828) ~[mongodb-driver-3.4.3.jar:na]
        at com.mongodb.MongoCollectionImpl.insertMany(MongoCollectionImpl.java:338) ~[mongodb-driver-3.4.3.jar:na]
        at com.mongodb.MongoCollectionImpl.insertMany(MongoCollectionImpl.java:322) ~[mongodb-driver-3.4.3.jar:na]
        at org.apache.camel.component.mongodb.MongoDbProducer.lambda$createDoInsert$7(MongoDbProducer.java:410) ~[camel-mongodb-2.20.0.jar:2.20.0]
        at org.apache.camel.component.mongodb.MongoDbProducer.lambda$wrap$0(MongoDbProducer.java:231) ~[camel-mongodb-2.20.0.jar:2.20.0]
        at org.apache.camel.component.mongodb.MongoDbProducer.invokeOperation(MongoDbProducer.java:112) ~[camel-mongodb-2.20.0.jar:2.20.0]
        at org.apache.camel.component.mongodb.MongoDbProducer.process(MongoDbProducer.java:96) ~[camel-mongodb-2.20.0.jar:2.20.0]
        ... 28 common frames omitted

Then I need to write:

 .marshal(jackson) 
        .convertBodyTo(String.class) 

but looks like producerTemplate doesn't have appropriate API.

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • You don't actually create a "key" named `$date` to store a date value, as that is just an "internal" representation of a `BSON Date` Type when "serialized". Simply provide any expression that returns a `java.util.Date` "directly" to the value of the key to which you want it assigned. So your serialize is actually fine. The problem is you are assigning to `$date` as opposed to directly assigning to the corresponding key. – Neil Lunn Nov 07 '17 at 23:25
  • @Neil Lunn, sure I tried this. But in collection I see value : **"createdDate" : { "mydate" : "2017-11-08T12:35:19Z" },...** if I write myDate instead of $date – gstackoverflow Nov 08 '17 at 06:36
  • You don't use `$date` nonetheless. `$` prefixed fields are "reserved", hence the error. If it's going to "string" then you are not actually feeding `java.util.Date` like you should be. In brief `value.toDate()` is where it's the "right" type. Then you go and "date format to string", and that part is where you go wrong. – Neil Lunn Nov 08 '17 at 06:38
  • @Neil Lunn Also I tried to use getter which returns java.util.Date in my model class but at this case in mongo I see: **"createdDate" : NumberLong("1510134026474"),** and this behaviour was described in topic I refer in my question Thus, looks like you marked question mistakenly – gstackoverflow Nov 08 '17 at 06:42
  • Read the comment again. I already told you where you went wrong here. You're paying to much attention to typing when you should be reading. – Neil Lunn Nov 08 '17 at 06:42
  • @Neil Lunn, Does the second approach not correspond your advice? – gstackoverflow Nov 08 '17 at 06:46
  • I don't think there is anything to mistake in *"Invalid BSON field name $date)"* which is the tile of the question you asked. The answer is "don't name a property `$date`." Anything else is not actually related to the error in the question asked. Feel free to [ask a new question.](https://stackoverflow.com/questions/ask) – Neil Lunn Nov 08 '17 at 06:50
  • @Neil Lunn, I really missed your advice about format. But which format would be correct? – gstackoverflow Nov 08 '17 at 07:10

0 Answers0