Skip to content

Commit

Permalink
refactor: objectMapper의 모듈 JavaTimeModule로 교체
Browse files Browse the repository at this point in the history
  • Loading branch information
seulgi99 committed Aug 29, 2024
1 parent 0c15ab8 commit e4b1331
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand All @@ -28,7 +28,7 @@ public class ObjectMapperConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
JavaTimeModule module = new JavaTimeModule();

// LocalDate
module.addSerializer(LocalDate.class, new LocalDateSerializer());
Expand All @@ -43,6 +43,7 @@ public ObjectMapper objectMapper() {
module.addDeserializer(LocalTime.class, new LocalTimeDeserializer());

// ZonedDateTime
module.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer());
module.addDeserializer(ZonedDateTime.class, new ZonedDateTimeDeserializer());

mapper.registerModule(module);
Expand Down Expand Up @@ -114,6 +115,15 @@ public LocalTime deserialize(JsonParser jsonParser, DeserializationContext deser
}
}

public class ZonedDateTimeSerializer extends JsonSerializer<ZonedDateTime> {
@Override
public void serialize(ZonedDateTime value, JsonGenerator generator, SerializerProvider serializers)
throws IOException {
generator.writeString(
value.format(DateTimeFormatter.ofPattern(DATETIME).withZone(ZoneId.of("Asia/Seoul"))));
}
}

public class ZonedDateTimeDeserializer extends JsonDeserializer<ZonedDateTime> {
@Override
public ZonedDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
Expand Down

0 comments on commit e4b1331

Please sign in to comment.