From 1bae49e2c4bec2aa62fcd23397ae5bc4647afd43 Mon Sep 17 00:00:00 2001 From: vran Date: Sun, 6 Feb 2022 10:57:36 +0800 Subject: [PATCH] feat: change time serialize/deserialize pattern --- .../java/com/databasir/api/config/WebConfig.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/api/src/main/java/com/databasir/api/config/WebConfig.java b/api/src/main/java/com/databasir/api/config/WebConfig.java index 653a4ae..c903d15 100644 --- a/api/src/main/java/com/databasir/api/config/WebConfig.java +++ b/api/src/main/java/com/databasir/api/config/WebConfig.java @@ -1,10 +1,16 @@ package com.databasir.api.config; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; +import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.web.config.EnableSpringDataWebSupport; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import java.time.format.DateTimeFormatter; + @Configuration @EnableSpringDataWebSupport public class WebConfig extends WebMvcConfigurerAdapter { @@ -15,4 +21,14 @@ public class WebConfig extends WebMvcConfigurerAdapter { .allowedOrigins("*") .allowedMethods("GET", "POST", "DELETE", "PATCH", "PUT"); } + + @Bean + public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() { + final String dateTimeFormat = "yyyy-MM-dd HH:mm:ss"; + return builder -> { + builder.simpleDateFormat(dateTimeFormat); + builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateTimeFormat))); + builder.deserializers(new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(dateTimeFormat))); + }; + } } \ No newline at end of file