尝试将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 for 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);
}发布于 2016-12-14 15:51:05
看起来Spring支持除ZonedDateTime转换器之外的所有java时间转换器。您可以按以下方式注册一个。
@Bean
public CustomConversions customConversions(){
List<Converter<?,?>> converters = new ArrayList<>();
converters.add(new DateToZonedDateTimeConverter());
converters.add(new ZonedDateTimeToDateConverter());
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);
}
class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {
@Override
public ZonedDateTime convert(Date source) {
return source == null ? null : ofInstant(source.toInstant(), systemDefault());
}
}
class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {
@Override
public Date convert(ZonedDateTime source) {
return source == null ? null : Date.from(source.toInstant());
}
}另一种替代解决方案是只使用ZonedDateTime并在持久化到MongoDB时更改它。您可以轻松地将其从日期恢复回分区日期时间,同时取取。
下面是帮助转换的相关方法。
ZoneId zoneID = ZoneId.of("America/Chicago");从ZonedDateTime到java util日期。
Instant instant = Instant.now();
ZonedDateTime zonedDateTime = instant.atZone(zoneId);
Date date = Date.from(zdt.toInstant());从日期到ZonedDateTime
Instant instant = date.toInstant();
ZonedDateTime zonedDateTime = instant.atZone(zoneId);另一种选择是实现自定义编解码器以帮助进行转换。我为YearMonth创建了一个过滤器,用于过滤来自Mongo文档的YearMonth。如果读者想为分区日期时间创建自定义编解码器,我将把它作为练习留给读者。
您可以将下面的库用于基于编解码器的方法。
发布于 2018-01-26 21:00:21
在花了太多时间调试这个程序之后,我终于找到了一个最新版本的spring / spring数据的工作解决方案。目前,这在SpringBoot2.0.0.M7上适用于我。
从维拉姆那得到了公认的答案,我得到了Couldn't find PersistentEntity for type
我希望这能帮助人们避免掉进兔子洞。
@Configuration
public class MongoConfiguration {
@Bean
public MongoCustomConversions customConversions(){
List<Converter<?,?>> converters = new ArrayList<>();
converters.add(DateToZonedDateTimeConverter.INSTANCE);
converters.add( ZonedDateTimeToDateConverter.INSTANCE);
return new MongoCustomConversions(converters);
}
enum DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {
INSTANCE;
@Override
public ZonedDateTime convert(Date source) {
return ofInstant(source.toInstant(), systemDefault());
}
}
enum ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {
INSTANCE;
@Override
public Date convert(ZonedDateTime source) {
return Date.from(source.toInstant());
}
}
}https://stackoverflow.com/questions/41127665
复制相似问题