使用 Java 中的自定义 CodecProvider 反序列化来自 mongodb 的日期给出空结果

Deserializing Dates from mongodb with custom CodecProvider in Java gives null results

我已经实现了自定义 MongoDB CodecProvider to map to my java objects, using this Github gist。但是,我无法反序列化 Date 值,而是返回 null 值。这是我的 pojo 自定义编码器实现的片段 - AuditLog:

    public void encode(BsonWriter writer, AuditLog value, EncoderContext encoderContext) {
    Document document = new Document();
    DateCodec dateCodec = new DateCodec();
    ObjectId id = value.getLogId();
    Date timestamp = value.getTimestamp();
    String deviceId = value.getDeviceId();
    String userId = value.getUserId();
    String requestId = value.getRequestId();
    String operationType = value.getOperationType();
    String message = value.getMessage();
    String serviceName = value.getServiceName();
    String className = value.getClassName();

    if (null != id) {
        document.put("_id", id);
    }
    if (null != timestamp) {
        document.put("timestamp", timestamp);
    }
    if (null != deviceId) {
        document.put("deviceId", deviceId);
    }
    if (null != userId) {
        document.put("userId", userId);
    }
    if (null != requestId) {
        document.put("requestId", requestId);
    }
    if (null != operationType) {
        document.put("operationType", operationType);
    }
    if (null != message) {
        document.put("message", message);
    }
    if (null != serviceName) {
        document.put("serviceName", serviceName);
    }
    if (null != className) {
        document.put("className", className);
    }

    documentCodec.encode(writer, document, encoderContext);

}

和解码器:

public AuditLog decode(BsonReader reader, DecoderContext decoderContext) {
    Document document = documentCodec.decode(reader, decoderContext);
    System.out.println("document " + document);

    AuditLog auditLog = new AuditLog();

    auditLog.setLogId(document.getObjectId("_id"));
    auditLog.setTimestamp(document.getDate("timestamp"));
    auditLog.setDeviceId(document.getString("deviceId"));
    auditLog.setUserId(document.getString("userId"));
    auditLog.setRequestId(document.getString("requestId"));
    auditLog.setOperationType(document.getString("operationType"));
    auditLog.setMessage(document.getString("message"));
    auditLog.setServiceName(document.getString("serviceName"));
    auditLog.setClassName(document.getString("className"));

    return auditLog;
}

以及我阅读的方式:

public void getAuthenticationEntries() {

    Codec<Document> defaultDocumentCodec = MongoClient.getDefaultCodecRegistry().get(Document.class);

    AuditLogCodec auditLogCodec = new AuditLogCodec(defaultDocumentCodec);

    CodecRegistry codecRegistry = CodecRegistries.fromRegistries(MongoClient.getDefaultCodecRegistry(),
            CodecRegistries.fromCodecs(auditLogCodec));

    MongoClientOptions options = MongoClientOptions.builder().codecRegistry(codecRegistry).build();

    MongoClient mc = new MongoClient("1.2.3.4:27017", options);
    MongoCollection<AuditLog> collection = mc.getDatabase("myDB").getCollection("myCol",
            AuditLog.class);
    BasicDBObject neQuery = new BasicDBObject();

    neQuery.put("myFiltr", new BasicDBObject("$eq", "mystuffr"));

    FindIterable<AuditLog> cursor = collection.find(neQuery);
    List<AuditLog> cleanList = new ArrayList<AuditLog>();
    for (AuditLog object : cursor) {

        System.out.println("timestamp: " + object.getTimestamp());

    }

}

我的POJO:

public class AuditLog implements Bson {

@Id
private ObjectId  logId;

@JsonProperty("@timestamp")
private Date timestamp;
@JsonProperty("deviceId")
private String deviceId;
@JsonProperty("userId")
private String userId;
@JsonProperty("requestId")
private String requestId;
@JsonProperty("operationType")
private String operationType;

@JsonProperty("message")
private String message;
@JsonProperty("serviceName")
private String serviceName;
@JsonProperty("className")
private String className;

经过深入研究,我解决了返回 null 值的问题。 mongoimport 命令用于将日志文件从 elasticsearch 导入到 Mongodb。但是,在导入操作期间时间格式未转换为 ISODate。我要做的是使用以下命令将时间格式更新为 ISODate:

db.Collection.find().forEach(function (doc){  
doc.time = Date(time);
}); 
   db.dummy.save(doc);

Here 是一个解决类似挑战的相关问题。