亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了在Spring Data MongoDB for ZonedDateTime中注冊(cè)一個(gè)可審核的新日期轉(zhuǎn)換器的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我希望我的可審核(@CreatedDate@LastModifiedDate)MongoDB文檔使用ZonedDateTime字段。

顯然,Spring Data不支持此類型(請(qǐng)查看org.springframework.data.auditing.AnnotationAuditingMetadata)。

框架版本:Spring Boot 2.0.0Spring Data MongoDB 2.0.0

Spring數(shù)據(jù)審核錯(cuò)誤:

java.lang.IllegalArgumentException: Invalid date type for member <MEMBER NAME>!
Supported types are [org.joda.time.DateTime, org.joda.time.LocalDateTime, java.util.Date, java.lang.Long, long].

Mongo配置:

@Configuration
@EnableMongoAuditing
public class MongoConfiguration {

}

可審計(jì)實(shí)體:

public abstract class BaseDocument {

    @CreatedDate
    private ZonedDateTime createdDate;

    @LastModifiedDate
    private ZonedDateTime lastModifiedDate;

}

我嘗試的內(nèi)容

我還嘗試為ZonedDateTime創(chuàng)建自定義轉(zhuǎn)換器,但Spring data不考慮它。類DateConvertingAuditableBeanWrapper有一個(gè)ConversionService,它在構(gòu)造函數(shù)方法中配置了JodaTimeConvertersJsr310ConvertersThreeTenBackPortConverters。

自定義轉(zhuǎn)換器:

@Component
public class LocalDateTimeToZonedDateTimeConverter implements Converter<LocalDateTime, ZonedDateTime> {

    @Override
    public ZonedDateTime convert(LocalDateTime source) {
        return source.atZone(ZoneId.systemDefault());
    }

}

Spring Data DateConvertingAuditableBeanWrapper:

class DefaultAuditableBeanWrapperFactory implements AuditableBeanWrapperFactory {

    abstract static class DateConvertingAuditableBeanWrapper implements AuditableBeanWrapper {

        private final ConversionService conversionService;

    }
}

是否可以審核ZonedDateTime個(gè)字段?

如何注冊(cè)轉(zhuǎn)換器?

推薦答案

創(chuàng)建DateTimeProvider以提供審核時(shí)使用的當(dāng)前時(shí)間:

@Component("dateTimeProvider")
public class CustomDateTimeProvider implements DateTimeProvider {
    
    @Override
    public Optional<TemporalAccessor> getNow() {
        return Optional.of(ZonedDateTime.now());
    }
}

然后:

引用@EnableMongoAuditing批注中的DateTimeProvider組件;
DateZonedDateTime創(chuàng)建Converter;
Converter實(shí)例添加到MongoCustomConversions實(shí)例;
MongoCustomConversions實(shí)例公開(kāi)為@Bean

@Configuration
@EnableMongoAuditing(dateTimeProviderRef = "dateTimeProvider")
public class MongoConfiguration {
    
    @Bean
    public MongoCustomConversions customConversions() {
        List<Converter<?, ?>> converters = new ArrayList<>();
        converters.add(new DateToZonedDateTimeConverter());
        converters.add(new ZonedDateTimeToDateConverter());
        return new MongoCustomConversions(converters);
    }

    class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {

        @Override
        public ZonedDateTime convert(Date source) {
            return source == null ? null : 
                    ZonedDateTime.ofInstant(source.toInstant(), ZoneId.systemDefault());
        }
    }

    class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {

        @Override
        public Date convert(ZonedDateTime source) {
            return source == null ? null : Date.from(source.toInstant());
        }
    }
}

但是,我不會(huì)將ZonedDateTime用于此目的。我會(huì)堅(jiān)持OffsetDateTime

OffsetDateTimeZonedDateTimeInstant都以納秒精度存儲(chǔ)時(shí)間線上的瞬間。Instant是最簡(jiǎn)單的,簡(jiǎn)單地表示瞬間。OffsetDateTime將UTC/格林威治的偏移量添加到該時(shí)刻,這允許獲取本地日期-時(shí)間。ZonedDateTime添加完整時(shí)區(qū)規(guī)則。

計(jì)劃使用ZonedDateTimeInstant對(duì)更簡(jiǎn)單的應(yīng)用程序中的數(shù)據(jù)進(jìn)行建模。在對(duì)日期-時(shí)間概念進(jìn)行更詳細(xì)的建模時(shí),或者在與數(shù)據(jù)庫(kù)或網(wǎng)絡(luò)協(xié)議進(jìn)行通信時(shí),可以使用此類。

這篇關(guān)于在Spring Data MongoDB for ZonedDateTime中注冊(cè)一個(gè)可審核的新日期轉(zhuǎn)換器的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,

分享到:
標(biāo)簽:Data Spring ZonedDateTime 審核 日期 注冊(cè) 轉(zhuǎn)換器
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定