-
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.
- Loading branch information
1 parent
618ce88
commit 0b33af7
Showing
2 changed files
with
49 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
47 changes: 47 additions & 0 deletions
47
...ava/com/jmsoftware/maf/reactivespringcloudstarter/configuration/JacksonConfiguration.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,47 @@ | ||
package com.jmsoftware.maf.reactivespringcloudstarter.configuration; | ||
|
||
import cn.hutool.core.util.StrUtil; | ||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; | ||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; | ||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; | ||
import com.jmsoftware.maf.common.constant.UniversalDateTime; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.val; | ||
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 JacksonConfiguration { | ||
private final JacksonProperties jacksonProperties; | ||
|
||
@Bean | ||
public LocalDateTimeSerializer localDateTimeSerializer() { | ||
var pattern = UniversalDateTime.DATE_TIME_FORMAT; | ||
if (StrUtil.isNotBlank(this.jacksonProperties.getDateFormat())) { | ||
pattern = this.jacksonProperties.getDateFormat(); | ||
} | ||
return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)); | ||
} | ||
|
||
@Bean | ||
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(LocalDateTimeSerializer localDateTimeSerializer) { | ||
var pattern = UniversalDateTime.DATE_TIME_FORMAT; | ||
if (StrUtil.isNotBlank(this.jacksonProperties.getDateFormat())) { | ||
pattern = this.jacksonProperties.getDateFormat(); | ||
} | ||
val localDateTimeDeserializer = new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(pattern)); | ||
return jackson2ObjectMapperBuilder -> jackson2ObjectMapperBuilder | ||
.serializerByType(LocalDateTime.class, localDateTimeSerializer) | ||
.deserializerByType(LocalDateTime.class, localDateTimeDeserializer) | ||
.serializerByType(Long.class, ToStringSerializer.instance); | ||
} | ||
} |