<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
首先第一點需要知道的是springboot預設依賴的json框架是jackson。
當使用@ResponseBody註解返回json格式資料時就是該框架在起作用。
如果欄位屬性是Date而非LocalDateTime時,通常我們會在application.properties裡面設定如下:
spring.mvc.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8 spring.jackson.serialization.write-dates-as-timestamps=false
如下圖所示,spring.jackson開頭的設定會被JacksonProperties類獲取進行使用。
當返回json格式的時候,Jackson就會根據組態檔中日期格式化的設定對結果進行處理。
但是如果欄位屬性為LocalDateTime呢?這種設定就失去了作用。
這時候建議設定如下:
/** * Created by jianggc at 2020/7/1. */ @Configuration public class LocalDateTimeSerializerConfig { @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}") private String pattern; // localDateTime 序列化器 @Bean public LocalDateTimeSerializer localDateTimeSerializer() { return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)); } // localDateTime 反序列化器 @Bean public LocalDateTimeDeserializer localDateTimeDeserializer() { return new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(pattern)); } @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { // return new Jackson2ObjectMapperBuilderCustomizer() { // @Override // public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) { jacksonObjectMapperBuilder.featuresToDisable(SerializationFeature.FAIL_ON_EMPTY_BEANS); // jacksonObjectMapperBuilder.serializerByType(LocalDateTime.class, localDateTimeSerializer()); // jacksonObjectMapperBuilder.deserializerByType(LocalDateTime.class,localDateTimeDeserializer()); // } // }; //這種方式同上 return builder -> { builder.serializerByType(LocalDateTime.class, localDateTimeSerializer()); builder.deserializerByType(LocalDateTime.class,localDateTimeDeserializer()); builder.simpleDateFormat(pattern); }; } }
這種設定方式自然是全域性的,如果想針對某個欄位特殊處理,可以在類欄位上面新增註解@JsonFormat:
@JsonFormat( pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8") private Date createdDate; @JsonFormat( pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createdTime;
如下所示,前臺傳參2020-08-30 11:11:11,後臺使用LocalDateTime 接收。
通常會報錯類似如下:
nested exception is org.springframework.core.convert.ConversionFailedException:
Failed to convert from type [java.lang.String] to type [java.time.LocalDateTime ]
很顯然是在引數繫結的時候沒有找到合適的轉換器把String轉換為對應的格式。
@Bean public Converter<String, LocalDateTime> localDateTimeConvert() { return new Converter<String, LocalDateTime>() { @Override public LocalDateTime convert(String source) { DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime dateTime = null; try { //2020-01-01 00:00:00 switch (source.length()){ case 10: logger.debug("傳過來的是日期格式:{}",source); source=source+" 00:00:00"; break; case 13: logger.debug("傳過來的是日期 小時格式:{}",source); source=source+":00:00"; break; case 16: logger.debug("傳過來的是日期 小時:分鐘格式:{}",source); source=source+":00"; break; } dateTime = LocalDateTime.parse(source, df); } catch (Exception e) { logger.error(e.getMessage(),e); } return dateTime; } }; }
實現原理簡要描述
在進行引數繫結的時候,會使用WebDataBinder物件。而建立WebDataBinder物件時,會遍歷DefaultDataBinderFactory.initializer,使用其WebBindingInitializer initializer對WebDataBinder物件進行初始化。
初始化方法具體可見ConfigurableWebBindingInitializer.initBinder(WebDataBinder binder),原始碼如下:
public void initBinder(WebDataBinder binder) { binder.setAutoGrowNestedPaths(this.autoGrowNestedPaths); if (this.directFieldAccess) { binder.initDirectFieldAccess(); } //設定messageCodesResolver if (this.messageCodesResolver != null) { binder.setMessageCodesResolver(this.messageCodesResolver); } //設定bindingErrorProcessor if (this.bindingErrorProcessor != null) { binder.setBindingErrorProcessor(this.bindingErrorProcessor); } //設定validator if (this.validator != null && binder.getTarget() != null && this.validator.supports(binder.getTarget().getClass())) { binder.setValidator(this.validator); } //設定conversionService if (this.conversionService != null) { binder.setConversionService(this.conversionService); } if (this.propertyEditorRegistrars != null) { PropertyEditorRegistrar[] var2 = this.propertyEditorRegistrars; int var3 = var2.length; for(int var4 = 0; var4 < var3; ++var4) { PropertyEditorRegistrar propertyEditorRegistrar = var2[var4]; propertyEditorRegistrar.registerCustomEditors(binder); } } }
而conversionService中包含了許多的convert-型別格式化器。在WebDataBinder進行引數繫結的時候就會使用不同的格式化器即不同的convert進行引數型別轉換。
關於引數繫結的過程,有興趣的可以跟蹤DataBinder.doBind方法,在這個過程中會對前臺傳輸的值進行型別轉換為目標引數需要的型別。自定義的localDateTimeConvert也是在這裡被用到的。
如下所示前臺傳String格式給後臺引數endDate,引數型別為java.time.LocalDateTime。
找到我們自定義的converter
呼叫convert進行型別轉換:
可以看到轉換後的結果為:
/** * yyyy-MM-dd HH:mm:ss String-localDateTime * @return */ @Bean public Formatter<LocalDateTime> localDateTimeFormatter() { return new Formatter<LocalDateTime>() { @Override public LocalDateTime parse(String text, Locale locale) throws ParseException { return LocalDateTime.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); } @Override public String print(LocalDateTime localDateTime, Locale locale) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); return formatter.format(localDateTime); } }; }
自定義的格式化器會在SpringBoot啟動時自動化設定過程中被加入,具體可以參考如下程式碼。
WebMvcAutoConfiguration.mvcConversionService:
@Bean @Override public FormattingConversionService mvcConversionService() { WebConversionService conversionService = new WebConversionService(this.mvcProperties.getDateFormat()); addFormatters(conversionService); return conversionService; }
其會走到SpringApplication.configureEnvironment方法處:
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) { if (this.addConversionService) { //從這裡跟蹤 ConversionService conversionService = ApplicationConversionService.getSharedInstance(); environment.setConversionService((ConfigurableConversionService)conversionService); } this.configurePropertySources(environment, args); this.configureProfiles(environment, args); }
ApplicationConversionService.getSharedInstance如下所示,這裡可以看到其使用了設計模式中的懶漢式之雙重校驗鎖來獲取單例。
public static ConversionService getSharedInstance() { ApplicationConversionService sharedInstance = sharedInstance; if (sharedInstance == null) { Class var1 = ApplicationConversionService.class; synchronized(ApplicationConversionService.class) { sharedInstance = sharedInstance; if (sharedInstance == null) { sharedInstance = new ApplicationConversionService(); sharedInstance = sharedInstance; } } } return sharedInstance; }
繼續物件建立過程會發現其走到了configure處:
public ApplicationConversionService(StringValueResolver embeddedValueResolver) { if (embeddedValueResolver != null) { this.setEmbeddedValueResolver(embeddedValueResolver); } //我們從這裡繼續跟進 configure(this); }
這裡我們順帶看一下ApplicationConversionService的類繼承示意圖(其不只是可以作為ConversionService還可以作為ConverterRegistry與FormatterRegistry):
建立ApplicationConversionService時會對其進行設定,這裡很重要。其會注入預設的Converter和Formatter:
public static void configure(FormatterRegistry registry) { DefaultConversionService.addDefaultConverters(registry); DefaultFormattingConversionService.addDefaultFormatters(registry); addApplicationFormatters(registry); addApplicationConverters(registry); }
該方法執行完,會新增52個型別轉換器:
public static void addDefaultConverters(ConverterRegistry converterRegistry) { addScalarConverters(converterRegistry); addCollectionConverters(converterRegistry); converterRegistry.addConverter(new ByteBufferConverter((ConversionService) converterRegistry)); converterRegistry.addConverter(new StringToTimeZoneConverter()); converterRegistry.addConverter(new ZoneIdToTimeZoneConverter()); converterRegistry.addConverter(new ZonedDateTimeToCalendarConverter()); converterRegistry.addConverter(new ObjectToObjectConverter()); converterRegistry.addConverter(new IdToEntityConverter((ConversionService) converterRegistry)); converterRegistry.addConverter(new FallbackObjectToStringConverter()); converterRegistry.addConverter(new ObjectToOptionalConverter((ConversionService) converterRegistry)); }
addScalarConverters(converterRegistry);如下所示:
private static void addScalarConverters(ConverterRegistry converterRegistry) { converterRegistry.addConverterFactory(new NumberToNumberConverterFactory()); converterRegistry.addConverterFactory(new StringToNumberConverterFactory()); converterRegistry.addConverter(Number.class, String.class, new ObjectToStringConverter()); converterRegistry.addConverter(new StringToCharacterConverter()); converterRegistry.addConverter(Character.class, String.class, new ObjectToStringConverter()); converterRegistry.addConverter(new NumberToCharacterConverter()); converterRegistry.addConverterFactory(new CharacterToNumberFactory()); converterRegistry.addConverter(new StringToBooleanConverter()); converterRegistry.addConverter(Boolean.class, String.class, new ObjectToStringConverter()); converterRegistry.addConverterFactory(new StringToEnumConverterFactory()); converterRegistry.addConverter(new EnumToStringConverter((ConversionService) converterRegistry)); converterRegistry.addConverterFactory(new IntegerToEnumConverterFactory()); converterRegistry.addConverter(new EnumToIntegerConverter((ConversionService) converterRegistry)); converterRegistry.addConverter(new StringToLocaleConverter()); converterRegistry.addConverter(Locale.class, String.class, new ObjectToStringConverter()); converterRegistry.addConverter(new StringToCharsetConverter()); converterRegistry.addConverter(Charset.class, String.class, new ObjectToStringConverter()); converterRegistry.addConverter(new StringToCurrencyConverter()); converterRegistry.addConverter(Currency.class, String.class, new ObjectToStringConverter()); converterRegistry.addConverter(new StringToPropertiesConverter()); converterRegistry.addConverter(new PropertiesToStringConverter()); converterRegistry.addConverter(new StringToUUIDConverter()); converterRegistry.addConverter(UUID.class, String.class, new ObjectToStringConverter()); }
這裡會新增23個型別轉換器:
新增集合處理的型別轉換器(這裡會新增17個型別轉換器):
public static void addCollectionConverters(ConverterRegistry converterRegistry) { ConversionService conversionService = (ConversionService) converterRegistry; converterRegistry.addConverter(new ArrayToCollectionConverter(conversionService)); converterRegistry.addConverter(new CollectionToArrayConverter(conversionService)); converterRegistry.addConverter(new ArrayToArrayConverter(conversionService)); converterRegistry.addConverter(new CollectionToCollectionConverter(conversionService)); converterRegistry.addConverter(new MapToMapConverter(conversionService)); converterRegistry.addConverter(new ArrayToStringConverter(conversionService)); converterRegistry.addConverter(new StringToArrayConverter(conversionService)); converterRegistry.addConverter(new ArrayToObjectConverter(conversionService)); converterRegistry.addConverter(new ObjectToArrayConverter(conversionService)); converterRegistry.addConverter(new CollectionToStringConverter(conversionService)); converterRegistry.addConverter(new StringToCollectionConverter(conversionService)); converterRegistry.addConverter(new CollectionToObjectConverter(conversionService)); converterRegistry.addConverter(new ObjectToCollectionConverter(conversionService)); converterRegistry.addConverter(new StreamConverter(conversionService)); }
/** * Add formatters appropriate for most environments: including number formatters, * JSR-354 Money & Currency formatters, JSR-310 Date-Time and/or Joda-Time formatters, * depending on the presence of the corresponding API on the classpath. * @param formatterRegistry the service to register default formatters with */ public static void addDefaultFormatters(FormatterRegistry formatterRegistry) { // Default handling of number values formatterRegistry.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory()); // Default handling of monetary values if (jsr354Present) { formatterRegistry.addFormatter(new CurrencyUnitFormatter()); formatterRegistry.addFormatter(new MonetaryAmountFormatter()); formatterRegistry.addFormatterForFieldAnnotation(new Jsr354NumberFormatAnnotationFormatterFactory()); } // Default handling of date-time values // just handling JSR-310 specific date and time types new DateTimeFormatterRegistrar().registerFormatters(formatterRegistry); if (jodaTimePresent) { // handles Joda-specific types as well as Date, Calendar, Long new JodaTimeFormatterRegistrar().registerFormatters(formatterRegistry); } else { // regular DateFormat-based Date, Calendar, Long converters new DateFormatterRegistrar().registerFormatters(formatterRegistry); } }
DateTimeFormatterRegistrar.registerFormatters
@Override public void registerFormatters(FormatterRegistry registry) { DateTimeConverters.registerConverters(registry); DateTimeFormatter df = getFormatter(Type.DATE); DateTimeFormatter tf = getFormatter(Type.TIME); DateTimeFormatter dtf = getFormatter(Type.DATE_TIME); // Efficient ISO_LOCAL_* variants for printing since they are twice as fast... registry.addFormatterForFieldType(LocalDate.class, new TemporalAccessorPrinter( df == DateTimeFormatter.ISO_DATE ? DateTimeFormatter.ISO_LOCAL_DATE : df), new TemporalAccessorParser(LocalDate.class, df)); registry.addFormatterForFieldType(LocalTime.class, new TemporalAccessorPrinter( tf == DateTimeFormatter.ISO_TIME ? DateTimeFormatter.ISO_LOCAL_TIME : tf), new TemporalAccessorParser(LocalTime.class, tf)); registry.addFormatterForFieldType(LocalDateTime.class, new TemporalAccessorPrinter( dtf == DateTimeFormatter.ISO_DATE_TIME ? DateTimeFormatter.ISO_LOCAL_DATE_TIME : dtf), new TemporalAccessorParser(LocalDateTime.class, dtf)); registry.addFormatterForFieldType(ZonedDateTime.class, new TemporalAccessorPrinter(dtf), new TemporalAccessorParser(ZonedDateTime.class, dtf)); registry.addFormatterForFieldType(OffsetDateTime.class, new TemporalAccessorPrinter(dtf), new TemporalAccessorParser(OffsetDateTime.class, dtf)); registry.addFormatterForFieldType(OffsetTime.class, new TemporalAccessorPrinter(tf), new TemporalAccessorParser(OffsetTime.class, tf)); registry.addFormatterForFieldType(Instant.class, new InstantFormatter()); registry.addFormatterForFieldType(Period.class, new PeriodFormatter()); registry.addFormatterForFieldType(Duration.class, new DurationFormatter()); registry.addFormatterForFieldType(Year.class, new YearFormatter()); registry.addFormatterForFieldType(Month.class, new MonthFormatter()); registry.addFormatterForFieldType(YearMonth.class, new YearMonthFormatter()); registry.addFormatterForFieldType(MonthDay.class, new MonthDayFormatter()); registry.addFormatterForFieldAnnotation(new Jsr310DateTimeFormatAnnotationFormatterFactory()); }
DateTimeConverters.registerConverters
public static void registerConverters(ConverterRegistry registry) { DateFormatterRegistrar.addDateConverters(registry); registry.addConverter(new LocalDateTimeToLocalDateConverter()); registry.addConverter(new LocalDateTimeToLocalTimeConverter()); registry.addConverter(new ZonedDateTimeToLocalDateConverter()); registry.addConverter(new ZonedDateTimeToLocalTimeConverter()); registry.addConverter(new ZonedDateTimeToLocalDateTimeConverter()); registry.addConverter(new ZonedDateTimeToOffsetDateTimeConverter()); registry.addConverter(new ZonedDateTimeToInstantConverter()); registry.addConverter(new OffsetDateTimeToLocalDateConverter()); registry.addConverter(new OffsetDateTimeToLocalTimeConverter()); registry.addConverter(new OffsetDateTimeToLocalDateTimeConverter()); registry.addConverter(new OffsetDateTimeToZonedDateTimeConverter()); registry.addConverter(new OffsetDateTimeToInstantConverter()); registry.addConverter(new CalendarToZonedDateTimeConverter()); registry.addConverter(new CalendarToOffsetDateTimeConverter()); registry.addConverter(new CalendarToLocalDateConverter()); registry.addConverter(new CalendarToLocalTimeConverter()); registry.addConverter(new CalendarToLocalDateTimeConverter()); registry.addConverter(new CalendarToInstantConverter()); registry.addConverter(new LongToInstantConverter()); registry.addConverter(new InstantToLongConverter()); }
DateFormatterRegistrar.addDateConverters
public static void addDateConverters(ConverterRegistry converterRegistry) { converterRegistry.addConverter(new DateToLongConverter()); converterRegistry.addConverter(new DateToCalendarConverter()); converterRegistry.addConverter(new CalendarToDateConverter()); converterRegistry.addConverter(new CalendarToLongConverter()); converterRegistry.addConverter(new LongToDateConverter()); converterRegistry.addConverter(new LongToCalendarConverter()); }
新增全域性格式化器:
public static void addApplicationFormatters(FormatterRegistry registry) { registry.addFormatter(new CharArrayFormatter()); registry.addFormatter(new InetAddressFormatter()); registry.addFormatter(new IsoOffsetFormatter()); }
新增全域性型別轉換器:
public static void addApplicationConverters(ConverterRegistry registry) { addDelimitedStringConverters(registry); registry.addConverter(new StringToDurationConverter()); registry.addConverter(new DurationToStringConverter()); registry.addConverter(new NumberToDurationConverter()); registry.addConverter(new DurationToNumberConverter()); registry.addConverter(new StringToDataSizeConverter()); registry.addConverter(new NumberToDataSizeConverter()); registry.addConverter(new StringToFileConverter()); registry.addConverterFactory(new LenientStringToEnumConverterFactory()); registry.addConverterFactory(new LenientBooleanToEnumConverterFactory()); } public static void addDelimitedStringConverters(ConverterRegistry registry) { ConversionService service = (ConversionService)registry; registry.addConverter(new ArrayToDelimitedStringConverter(service)); registry.addConverter(new CollectionToDelimitedStringConverter(service)); registry.addConverter(new DelimitedStringToArrayConverter(service)); registry.addConverter(new DelimitedStringToCollectionConverter(service)); }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45