@Slf4j
@Configuration
@ConditionalOnClass(ObjectMapper.class)
@ConditionalOnProperty(prefix = "custom.jackson", name = "enabled", matchIfMissing = true)
@EnableConfigurationProperties(CustomJacksonProperties.class)
@AutoConfigureAfter(JacksonAutoConfiguration.class)
public class CustomJacksonAutoConfiguration {
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(HntJacksonProperties properties) {
return builder -> {
/**
* Serialize LocalDateTime to 'yyyy.MM.dd HH:mm:ss'
*/
builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(properties.getDateTimeFormat())));
builder.serializerByType(LocalDate.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(properties.getDateFormat())));
builder.serializerByType(LocalTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(properties.getTimeFormat())));
/**
* Deserialize LocalDateTime from 'yyyy.MM.dd HH:mm:ss'
*/
builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(properties.getDateTimeFormat())));
builder.deserializerByType(LocalDate.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(properties.getDateFormat())));
builder.deserializerByType(LocalTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(properties.getTimeFormat())));
/**
* 알 수 없는 ENUM 값은 NULL 로 간주
*/
builder.featuresToEnable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
};
}
}
Leave a comment