Skip to content

Commit

Permalink
feat($Jackson): add DateTime serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Jun 27, 2021
1 parent 0a6b61a commit 69548c4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
RestTemplateConfiguration.class,
AsyncConfiguration.class,
RabbitmqConfiguration.class,
MinioConfiguration.class
MinioConfiguration.class,
LocalDateTimeSerializerConfiguration.class
})
public class MafAutoConfiguration {
@PostConstruct
Expand Down
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);
}
}

0 comments on commit 69548c4

Please sign in to comment.