ZonedDateTime with MongoDB
尝试与 一起使用。我能够保存,但是当我查看记录时,它有很多不必要的东西:ZonedDateTime
MongoDB
ZonedDateTime
MongoDB
> "timestamp" : {
> "dateTime" : ISODate("2016-12-13T13:45:53.991Z"),
> "offset" : {
> "_id" : "-05:00",
> "totalSeconds" : -18000
> },
> "zone" : {
> "_class" : "java.time.ZoneRegion",
> "_id" : "America/New_York",
> "rules" : {
> "standardTransitions" : [
> NumberLong(-2717650800)
> ],
> "standardOffsets" : [
> {
> "_id" : "-04:56:02",
> "totalSeconds" : -17762
> },
> {
> "_id" : "-05:00",
> "totalSeconds" : -18000
> }
> ],
> "savingsInstantTransitions" : [
> NumberLong(-2717650800),
> NumberLong(-1633280400),
> NumberLong(-1615140000),
> NumberLong(-1601830800),
> NumberLong(-1583690400),
> NumberLong(-1570381200),
> and so on....
另外,当我尝试检索相同的日期时,它会给我以下信息:
> org.springframework.data.mapping.model.MappingException: No property
> null found on entity class java.time.ZonedDateTime to bind constructor
> parameter to!
我在使用 .第一个问题是,我们可以在某个地方更改一些只会持续存在的设置吗?第二个问题,有没有类似的东西?LocalDateTime
ISODate
ZonedDateTime
Jsr310JpaConverters
mongodb
更新:参考以下问题,我创建自定义转换器并注册了它们,但是,问题仍然存在。Spring Data MongoDB with Java 8 LocalDate MappingException
public class ZonedDateTimeToLocalDateTimeConverter implements Converter<ZonedDateTime, LocalDateTime> {
@Override
public LocalDateTime convert(ZonedDateTime source) {
return source == null ? null : LocalDateTime.ofInstant(source.toInstant(), ZoneId
.systemDefault());
}
}
和
public class LocalDateTimeToZonedDateTimeConverter implements Converter<LocalDateTime,
ZonedDateTime> {
@Override
public ZonedDateTime convert(LocalDateTime source) {
return source == null ? null : ZonedDateTime.of(source, ZoneId.systemDefault());
}
}
按如下方式注册它们:
@Bean
public CustomConversions customConversions(){
List<Converter<?,?>> converters = new ArrayList<Converter<?,?>>();
converters.add(new ZonedDateTimeToLocalDateTimeConverter());
converters.add(new LocalDateTimeToZonedDateTimeConverter());
return new CustomConversions(converters);
}
@Bean
public MongoTemplate getMongoTemplate() throws UnknownHostException {
MappingMongoConverter converter = new MappingMongoConverter(
new DefaultDbRefResolver(getMongoDbFactory()), new MongoMappingContext());
converter.setCustomConversions(customConversions());
converter.afterPropertiesSet();
return new MongoTemplate(getMongoDbFactory(), converter);
}