-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($Jackson): add DateTime serializer
- Loading branch information
1 parent
0a6b61a
commit 69548c4
Showing
2 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...jmsoftware/maf/springcloudstarter/configuration/LocalDateTimeSerializerConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.jmsoftware.maf.springcloudstarter.configuration; | ||
|
||
import cn.hutool.core.util.StrUtil; | ||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; | ||
import org.springframework.boot.autoconfigure.jackson.JacksonProperties; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
/** | ||
* Description: LocalDateTimeSerializerConfiguration, change description here. | ||
* | ||
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 6/27/2021 3:57 PM | ||
**/ | ||
@RequiredArgsConstructor | ||
public class LocalDateTimeSerializerConfiguration { | ||
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; | ||
private final JacksonProperties jacksonProperties; | ||
|
||
@Bean | ||
public LocalDateTimeSerializer localDateTimeSerializer() { | ||
var pattern = DEFAULT_DATE_FORMAT; | ||
if (StrUtil.isNotBlank(jacksonProperties.getDateFormat())) { | ||
pattern = jacksonProperties.getDateFormat(); | ||
} | ||
return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)); | ||
} | ||
|
||
@Bean | ||
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(LocalDateTimeSerializer localDateTimeSerializer) { | ||
return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeSerializer); | ||
} | ||
} |