Created
March 30, 2017 10:31
-
-
Save abop/4be6d8b3538c18f720484a783811278c to your computer and use it in GitHub Desktop.
订制默认的 jackson mapper, 自定义 java 8 time api 中对象的序列化格式
This file contains hidden or 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
@Bean | |
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer(JacksonProperties jacksonProperties) { | |
return jacksonObjectMapperBuilder -> { | |
String dateFormatStr = jacksonProperties.getDateFormat(); | |
DateTimeFormatter formatter = | |
FanStringUtil.isNotBlank(dateFormatStr) ? DateTimeFormatter.ofPattern(dateFormatStr) : null; | |
jacksonObjectMapperBuilder.modules( | |
new JavaTimeModule() | |
.addSerializer(LocalDate.class, new LocalDateSerializer(formatter)) | |
.addSerializer(LocalTime.class, new LocalTimeSerializer(formatter)) | |
.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter)) | |
.addDeserializer(LocalDate.class, new LocalDateDeserializer(formatter)) | |
.addDeserializer(LocalTime.class, new LocalTimeDeserializer(formatter)) | |
.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter)) | |
); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment