简体   繁体   English

Spring 数据 MongoDB 字段转换器:有什么方法可以传递参数?

[英]Spring Data MongoDB Field Converter: Any way to pass a parameter?

When our application is loading a DBOject/Document instance from our MongoDB, we need to convert all of the UTC dates in it into ZoneDateTime values using the zoneIdName, which is within the DBObject/Document instance.当我们的应用程序从 MongoDB 加载 DBOject/Document 实例时,我们需要使用 DBObject/Document 实例中的 zoneIdName 将其中的所有 UTC 日期转换为 ZoneDateTime 值。 We have a lot of Date fields in the DBObject, so I'd like to avoid having to implement a DBOject level Spring Data MongoDB Converter.我们在 DBObject 中有很多日期字段,所以我想避免必须实现 DBOject 级别 Spring 数据 MongoDB 转换器。

Is it possible to use a DBOject field level Spring Data MongoDB Converter, such as the following, which uses a field in the DBObject (ie zoneIdName), to be able to perform the conversion to a ZoneDateTime?是否可以使用 DBOject 字段级别 Spring 数据 MongoDB 转换器,如下所示,它使用 DBObject 中的字段(即 zoneIdName),能够执行到 ZoneDateTime 的转换? If not, I will have to implement an object-level Converter, but it will be tedious...如果没有,我将不得不实现一个对象级转换器,但这会很乏味......

Thank you for your interest and time.感谢您的关注和时间。

class ZonedDateTimeReadConverter :
    Converter<Date, ZonedDateTime> {

    override fun convert(date: Date): ZonedDateTime {
        // Need to replace "America/LosAngeles" with 
        // a value from a field in the object being processed.
        return date.toInstant().atZone(ZoneId.of("America/LosAngeles"))
    }
}

seems that converter for whole object is only option似乎整个 object 的转换器是唯一的选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM