代码之家  ›  专栏  ›  技术社区  ›  SyCode

在Java中用自定义代码提供程序反序列化日期提供空结果

  •  0
  • SyCode  · 技术社区  · 6 年前

    我实现了一个自定义MongoDB CodecProvider 若要映射到我的Java对象,请使用 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;
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   SyCode    6 年前

    经过一番周密的研究,我解决了退货的问题。 null 价值观。这个 mongoimport 命令用于将日志文件导入 Mongodb elasticsearch . 但是,时间格式未转换为 ISODate 在导入操作期间。我要做的是使用以下命令将时间格式更新为isodate:

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

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