@Bean
public ObjectMapper objectMapper() {
var dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
var dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
var timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
var javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(dateTimeFormatter))
.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter))
.addSerializer(LocalDate.class, new LocalDateSerializer(dateFormatter))
.addDeserializer(LocalDate.class, new LocalDateDeserializer(dateFormatter))
.addSerializer(LocalTime.class, new LocalTimeSerializer(timeFormatter))
.addDeserializer(LocalTime.class, new LocalTimeDeserializer(timeFormatter))
.addSerializer(Instant.class, new JsonSerializer<>() {
@Override
public void serialize(Instant instant, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString(instant.atZone(ZoneOffset.of("+8")).format(dateTimeFormatter));
}
})
.addDeserializer(Instant.class, new JsonDeserializer<>() {
@Override
public Instant deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
return LocalDateTime.parse(jsonParser.getValueAsString(), dateTimeFormatter).toInstant(ZoneOffset.of("+8"));
}
});
return JsonMapper.builder()
.deactivateDefaultTyping()
.addModules(javaTimeModule)
.build();
}