Skip to content

Instantly share code, notes, and snippets.

@abop
Created March 30, 2017 10:31
Show Gist options
  • Save abop/4be6d8b3538c18f720484a783811278c to your computer and use it in GitHub Desktop.
Save abop/4be6d8b3538c18f720484a783811278c to your computer and use it in GitHub Desktop.
订制默认的 jackson mapper, 自定义 java 8 time api 中对象的序列化格式
@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