0

我有这样的pojo:

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

在一些代码库中,我有以下代码:

@Autowired
private ProducerTemplate producerTemplate;

...

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

但是当我在数据库中打开集合时,我看到这样的日期字段:

"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"
                }

但我想存储 mongoDB 可识别的常用 ISO 日期。它应该是这样的:

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

我是如何尝试解决这个问题的?

我读了这个话题

因此,我创建了一个自定义序列化程序:

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);
    }
}

并正确注册:

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

但它抛出异常:

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

然后我需要写:

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

但看起来producerTemplate没有合适的 API。

4

0 回答 0