Springboot中全局时间格式化操作的示例分析(springboot,开发技术)

时间:2024-05-09 06:59:09 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

时间格式化在项目中使用频率是非常高的,当我们的 API 接口返回结果,需要对其中某一个 date 字段属性进行特殊的格式化处理,通常会用到 SimpleDateFormat 工具处理。

SimpleDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-dd");DatestationTime=dateFormat.parse(dateFormat.format(PayEndTime()));

可一旦处理的地方较多,不仅 CV 操作频繁,还产生很多重复臃肿的代码,而此时如果能将时间格式统一配置,就可以省下更多时间专注于业务开发了。

可能很多人觉得统一格式化时间很简单啊,像下边这样配置一下就行了,但事实上这种方式只对 date 类型生效。

spring.jackson.date-format=yyyy-MM-ddHH:mm:ssspring.jackson.time-zone=GMT+8

而很多项目中用到的时间和日期API 比较混乱, java.util.Date 、 java.util.Calendar 和 java.time LocalDateTime 都存在,所以全局时间格式化必须要同时兼容性新旧 API。

看看配置全局时间格式化前,接口返回时间字段的格式。

@DatapublicclassOrderDTO{privateLocalDateTimecreateTime;privateDateupdateTime;}

很明显不符合页面上的显示要求(有人抬杠为啥不让前端解析时间,我只能说睡服代码比说服人容易得多~)

一、@JsonFormat 注解

@JsonFormat 注解方式严格意义上不能叫全局时间格式化,应该叫部分格式化,因为@JsonFormat 注解需要用在实体类的时间字段上,而只有使用相应的实体类,对应的字段才能进行格式化。

@DatapublicclassOrderDTO{@JsonFormat(locale="zh",timezone="GMT+8",pattern="yyyy-MM-dd")privateLocalDateTimecreateTime;@JsonFormat(locale="zh",timezone="GMT+8",pattern="yyyy-MM-ddHH:mm:ss")privateDateupdateTime;}

字段加上 @JsonFormat 注解后,LocalDateTime 和 Date 时间格式化成功。

二、@JsonComponent 注解(推荐)

这是我个人比较推荐的一种方式,前边看到使用 @JsonFormat 注解并不能完全做到全局时间格式化,所以接下来我们使用 @JsonComponent 注解自定义一个全局格式化类,分别对 Date 和 LocalDate 类型做格式化处理。

@JsonComponentpublicclassDateFormatConfig{@Value("${spring.jackson.date-format:yyyy-MM-ddHH:mm:ss}")privateStringpattern;/***@authorxiaofu*@descriptiondate类型全局时间格式化*@date2020/8/3118:22*/@BeanpublicJackson2ObjectMapperBuilderCustomizerjackson2ObjectMapperBuilder(){returnbuilder->{TimeZonetz=TimeZone.getTimeZone("UTC");DateFormatdf=newSimpleDateFormat(pattern);df.setTimeZone(tz);builder.failOnEmptyBeans(false).failOnUnknownProperties(false).featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).dateFormat(df);};}/***@authorxiaofu*@descriptionLocalDate类型全局时间格式化*@date2020/8/3118:22*/@BeanpublicLocalDateTimeSerializerlocalDateTimeDeserializer(){returnnewLocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));}@BeanpublicJackson2ObjectMapperBuilderCustomizerjackson2ObjectMapperBuilderCustomizer(){returnbuilder->builder.serializerByType(LocalDateTime.class,localDateTimeDeserializer());}}

看到 Date 和 LocalDate 两种时间类型格式化成功,此种方式有效。

@JsonComponent 注解处理格式化

但还有个问题,实际开发中如果我有个字段不想用全局格式化设置的时间样式,想自定义格式怎么办?

那就需要和 @JsonFormat 注解配合使用了。

@DatapublicclassOrderDTO{@JsonFormat(locale="zh",timezone="GMT+8",pattern="yyyy-MM-dd")privateLocalDateTimecreateTime;@JsonFormat(locale="zh",timezone="GMT+8",pattern="yyyy-MM-dd")privateDateupdateTime;}

从结果上我们看到 @JsonFormat 注解的优先级比较高,会以 @JsonFormat 注解的时间格式为主。

三、@Configuration 注解

这种全局配置的实现方式与上边的效果是一样的。

注意:在使用此种配置后,字段手动配置@JsonFormat 注解将不再生效。

@ConfigurationpublicclassDateFormatConfig2{@Value("${spring.jackson.date-format:yyyy-MM-ddHH:mm:ss}")privateStringpattern;publicstaticDateFormatdateFormat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");@Bean@PrimarypublicObjectMapperserializingObjectMapper(){ObjectMapperobjectMapper=newObjectMapper();JavaTimeModulejavaTimeModule=newJavaTimeModule();javaTimeModule.addSerializer(LocalDateTime.class,newLocalDateTimeSerializer());javaTimeModule.addDeserializer(LocalDateTime.class,newLocalDateTimeDeserializer());objectMapper.registerModule(javaTimeModule);returnobjectMapper;}/***@authorxiaofu*@descriptionDate时间类型装换*@date2020/9/117:25*/@ComponentpublicclassDateSerializerextendsJsonSerializer<Date>{@Overridepublicvoidserialize(Datedate,JsonGeneratorgen,SerializerProviderprovider)throwsIOException{StringformattedDate=dateFormat.format(date);gen.writeString(formattedDate);}}/***@authorxiaofu*@descriptionDate时间类型装换*@date2020/9/117:25*/@ComponentpublicclassDateDeserializerextendsJsonDeserializer<Date>{@OverridepublicDatedeserialize(JsonParserjsonParser,DeserializationContextdeserializationContext)throwsIOException{try{returndateFormat.parse(jsonParser.getValueAsString());}catch(ParseExceptione){thrownewRuntimeException("Couldnotparsedate",e);}}}/***@authorxiaofu*@descriptionLocalDate时间类型装换*@date2020/9/117:25*/publicclassLocalDateTimeSerializerextendsJsonSerializer<LocalDateTime>{@Overridepublicvoidserialize(LocalDateTimevalue,JsonGeneratorgen,SerializerProviderserializers)throwsIOException{gen.writeString(value.format(DateTimeFormatter.ofPattern(pattern)));}}/***@authorxiaofu*@descriptionLocalDate时间类型装换*@date2020/9/117:25*/publicclassLocalDateTimeDeserializerextendsJsonDeserializer<LocalDateTime>{@OverridepublicLocalDateTimedeserialize(JsonParserp,DeserializationContextdeserializationContext)throwsIOException{returnLocalDateTime.parse(p.getValueAsString(),DateTimeFormatter.ofPattern(pattern));}}}

SpringBoot全局日期格式转换失效问题记录

今天新搭建了一个项目, 像以前一样在一个配置类上做了个全局字符串转换日期对象的转换器!

@BeanpublicJackson2ObjectMapperBuilderCustomizerjackson2ObjectMapperBuilderCustomizer(){returnbuilder->{.............};}}//-----------------//或者使用官网提供的通过注解`@JsonComponent`来声明其静态内部类,都没生效!

但是发现失效了, 在debug模式下根本没有进来,没有任何反应! 后面发现原因在继承了WebMvcConfigurationSupport配置类,导致自动配置失效!

在网上找到的原因, 如下图所示:

Springboot中全局时间格式化操作的示例分析

自动配置在当WebMvcConfigurationSupport类不存在的时候才会生效WebMvc自动化配置,WebMvc自动配置类中不仅定义了classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/等路径的映射,还定义了配置文件spring.mvc开头的配置信息等。

类路径上的 HttpMessageConverter 失效

解决方案

是在自己继承了WebMvcConfigurationSupport上配置转换器:

@ConfigurationpublicclassWebMvcConfigextendsWebMvcConfigurationSupport{/***解决继承WebMvcConfigurationSupport,静态资源访问不到**@paramregistry*@author:ZhiHao*@date:2020/6/11*/@OverrideprotectedvoidaddResourceHandlers(ResourceHandlerRegistryregistry){registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");super.addResourceHandlers(registry);}/***解决继承WebMvcConfigurationSupport,json方式全局日期反序列化失效**@paramconverters*@author:ZhiHao*@date:2020/6/11*/@OverrideprotectedvoidconfigureMessageConverters(List<HttpMessageConverter<?>>converters){MappingJackson2HttpMessageConverterconverter=newMappingJackson2HttpMessageConverter();ObjectMapperobjectMapper=newObjectMapper();SimpleModulemodule=newSimpleModule();module.addDeserializer(Date.class,newJsonDeserializer<Date>(){@OverridepublicDatedeserialize(JsonParserjsonParser,DeserializationContextdeserializationContext)throwsIOException{returnDateConverterConfig.parse(jsonParser.getText());}});objectMapper.registerModule(module);converter.setObjectMapper(objectMapper);converters.add(converter);super.configureMessageConverters(converters);}/***解决继承WebMvcConfigurationSupport,普通请求,String转换Date-转换器**@paramregistry*@author:ZhiHao*@date:2020/6/11*/@OverrideprotectedvoidaddFormatters(FormatterRegistryregistry){registry.addConverter(newDateConverterConfig());}}
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Springboot中全局时间格式化操作的示例分析的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:python如何实现钉钉机器人自动打卡天天早下班下一篇:

5 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18