色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

Springboot 全局時(shí)間格式化操作

瀏覽:64日期:2023-03-06 16:57:00

時(shí)間格式化在項(xiàng)目中使用頻率是非常高的,當(dāng)我們的 API 接口返回結(jié)果,需要對(duì)其中某一個(gè) date 字段屬性進(jìn)行特殊的格式化處理,通常會(huì)用到 SimpleDateFormat 工具處理。

SimpleDateFormat dateFormat = new SimpleDateFormat('yyyy-MM-dd');Date stationTime = dateFormat.parse(dateFormat.format(PayEndTime()));

可一旦處理的地方較多,不僅 CV 操作頻繁,還產(chǎn)生很多重復(fù)臃腫的代碼,而此時(shí)如果能將時(shí)間格式統(tǒng)一配置,就可以省下更多時(shí)間專注于業(yè)務(wù)開發(fā)了。

可能很多人覺得統(tǒng)一格式化時(shí)間很簡單啊,像下邊這樣配置一下就行了,但事實(shí)上這種方式只對(duì) date 類型生效。

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

而很多項(xiàng)目中用到的時(shí)間和日期API 比較混亂, java.util.Date 、 java.util.Calendar 和 java.time LocalDateTime 都存在,所以全局時(shí)間格式化必須要同時(shí)兼容性新舊 API。

看看配置全局時(shí)間格式化前,接口返回時(shí)間字段的格式。

@Datapublic class OrderDTO { private LocalDateTime createTime; private Date updateTime;}

很明顯不符合頁面上的顯示要求(有人抬杠為啥不讓前端解析時(shí)間,我只能說睡服代碼比說服人容易得多~)

一、@JsonFormat 注解

@JsonFormat 注解方式嚴(yán)格意義上不能叫全局時(shí)間格式化,應(yīng)該叫部分格式化,因?yàn)锧JsonFormat 注解需要用在實(shí)體類的時(shí)間字段上,而只有使用相應(yīng)的實(shí)體類,對(duì)應(yīng)的字段才能進(jìn)行格式化。

@Datapublic class OrderDTO { @JsonFormat(locale = 'zh', timezone = 'GMT+8', pattern = 'yyyy-MM-dd') private LocalDateTime createTime; @JsonFormat(locale = 'zh', timezone = 'GMT+8', pattern = 'yyyy-MM-dd HH:mm:ss') private Date updateTime;}

字段加上 @JsonFormat 注解后,LocalDateTime 和 Date 時(shí)間格式化成功。

二、@JsonComponent 注解(推薦)

這是我個(gè)人比較推薦的一種方式,前邊看到使用 @JsonFormat 注解并不能完全做到全局時(shí)間格式化,所以接下來我們使用 @JsonComponent 注解自定義一個(gè)全局格式化類,分別對(duì) Date 和 LocalDate 類型做格式化處理。

@JsonComponentpublic class DateFormatConfig { @Value('${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}') private String pattern; /** * @author xiaofu * @description date 類型全局時(shí)間格式化 * @date 2020/8/31 18:22 */ @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilder() { return builder -> { TimeZone tz = TimeZone.getTimeZone('UTC'); DateFormat df = new SimpleDateFormat(pattern); df.setTimeZone(tz); builder.failOnEmptyBeans(false) .failOnUnknownProperties(false) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .dateFormat(df);}; } /** * @author xiaofu * @description LocalDate 類型全局時(shí)間格式化 * @date 2020/8/31 18:22 */ @Bean public LocalDateTimeSerializer localDateTimeDeserializer() {return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)); } @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer()); }}

看到 Date 和 LocalDate 兩種時(shí)間類型格式化成功,此種方式有效。

@JsonComponent 注解處理格式化

但還有個(gè)問題,實(shí)際開發(fā)中如果我有個(gè)字段不想用全局格式化設(shè)置的時(shí)間樣式,想自定義格式怎么辦?

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

@Datapublic class OrderDTO { @JsonFormat(locale = 'zh', timezone = 'GMT+8', pattern = 'yyyy-MM-dd') private LocalDateTime createTime; @JsonFormat(locale = 'zh', timezone = 'GMT+8', pattern = 'yyyy-MM-dd') private Date updateTime;}

從結(jié)果上我們看到 @JsonFormat 注解的優(yōu)先級(jí)比較高,會(huì)以 @JsonFormat 注解的時(shí)間格式為主。

三、@Configuration 注解

這種全局配置的實(shí)現(xiàn)方式與上邊的效果是一樣的。

注意:在使用此種配置后,字段手動(dòng)配置@JsonFormat 注解將不再生效。

@Configurationpublic class DateFormatConfig2 { @Value('${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}') private String pattern; public static DateFormat dateFormat = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss'); @Bean @Primary public ObjectMapper serializingObjectMapper() {ObjectMapper objectMapper = new ObjectMapper();JavaTimeModule javaTimeModule = new JavaTimeModule();javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());objectMapper.registerModule(javaTimeModule);return objectMapper; } /** * @author xiaofu * @description Date 時(shí)間類型裝換 * @date 2020/9/1 17:25 */ @Component public class DateSerializer extends JsonSerializer<Date> {@Overridepublic void serialize(Date date, JsonGenerator gen, SerializerProvider provider) throws IOException { String formattedDate = dateFormat.format(date); gen.writeString(formattedDate);} } /** * @author xiaofu * @description Date 時(shí)間類型裝換 * @date 2020/9/1 17:25 */ @Component public class DateDeserializer extends JsonDeserializer<Date> { @Overridepublic Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { try {return dateFormat.parse(jsonParser.getValueAsString()); } catch (ParseException e) {throw new RuntimeException('Could not parse date', e); }} } /** * @author xiaofu * @description LocalDate 時(shí)間類型裝換 * @date 2020/9/1 17:25 */ public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {@Overridepublic void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeString(value.format(DateTimeFormatter.ofPattern(pattern)));} } /** * @author xiaofu * @description LocalDate 時(shí)間類型裝換 * @date 2020/9/1 17:25 */ public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {@Overridepublic LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException { return LocalDateTime.parse(p.getValueAsString(), DateTimeFormatter.ofPattern(pattern));} }}SpringBoot全局日期格式轉(zhuǎn)換失效問題記錄

今天新搭建了一個(gè)項(xiàng)目, 像以前一樣在一個(gè)配置類上做了個(gè)全局字符串轉(zhuǎn)換日期對(duì)象的轉(zhuǎn)換器!

@Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {return builder -> { .............}; }}//-----------------//或者使用官網(wǎng)提供的 通過注解`@JsonComponent`來聲明其靜態(tài)內(nèi)部類,都沒生效!

但是發(fā)現(xiàn)失效了, 在debug模式下根本沒有進(jìn)來,沒有任何反應(yīng)! 后面發(fā)現(xiàn)原因在繼承了WebMvcConfigurationSupport配置類,導(dǎo)致自動(dòng)配置失效!

在網(wǎng)上找到的原因, 如下圖所示:

Springboot 全局時(shí)間格式化操作

自動(dòng)配置在當(dāng)WebMvcConfigurationSupport類不存在的時(shí)候才會(huì)生效WebMvc自動(dòng)化配置,WebMvc自動(dòng)配置類中不僅定義了classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/等路徑的映射,還定義了配置文件spring.mvc開頭的配置信息等。

類路徑上的 HttpMessageConverter 失效

解決方案

是在自己繼承了WebMvcConfigurationSupport上配置轉(zhuǎn)換器:

@Configurationpublic class WebMvcConfig extends WebMvcConfigurationSupport {/** * 解決繼承WebMvcConfigurationSupport,靜態(tài)資源訪問不到 * * @param registry * @author: ZhiHao * @date: 2020/6/11 */ @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler('/**').addResourceLocations('classpath:/static/');super.addResourceHandlers(registry); } /** * 解決繼承WebMvcConfigurationSupport, json方式全局日期反序列化失效 * * @param converters * @author: ZhiHao * @date: 2020/6/11 */ @Override protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();ObjectMapper objectMapper = new ObjectMapper();SimpleModule module = new SimpleModule();module.addDeserializer(Date.class, new JsonDeserializer<Date>() { @Override public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {return DateConverterConfig.parse(jsonParser.getText()); }});objectMapper.registerModule(module);converter.setObjectMapper(objectMapper);converters.add(converter);super.configureMessageConverters(converters); } /** * 解決繼承WebMvcConfigurationSupport, 普通請(qǐng)求,String轉(zhuǎn)換Date-轉(zhuǎn)換器 * * @param registry * @author: ZhiHao * @date: 2020/6/11 */ @Override protected void addFormatters(FormatterRegistry registry) {registry.addConverter(new DateConverterConfig()); }}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 久久国产乱子伦精品免费不卡 | 99精品热女视频专线 | 久草久热| 中国性孕妇孕交在线 | 亚洲欧美中文字幕在线网站 | 完整日本特级毛片 | 一级片国产 | 久草在线中文最新视频 | 成人午夜 | 亚洲国产精品第一区二区 | 欧美另类老妇 | 国产精品久久久久久久人热 | 美国毛片毛片全部免费 | 国产男女爽爽爽爽爽免费视频 | 成人欧美一区二区三区黑人免费 | 久久久久久免费播放一级毛片 | 玖草在线 | 日本高清在线中文字幕网 | 成人9久久国产精品品 | fc2久久| 免费a级毛片视频 | 香港经典毛片a免费观看 | 亚洲欧美另类日本久久影院 | 成人精品一区二区久久 | 亚洲欧美日韩国产精品影院 | 久久国产成人 | 一级a欧美毛片 | 在线观看视频亚洲 | 日本理论片免费高清影视在线观看 | 亚洲国产小视频 | 瑟瑟网站在线观看 | 久草在线视频资源站 | 男人女人做刺激视频免费 | 亚洲欧美中文日韩二区一区 | 91av观看| 久久九九精品一区二区 | 欧美性生交大片免费看 | 亚洲人成一区二区三区 | 欧美日韩国产va另类 | 一区二区三区视频 | 精品在线视频一区 |