Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ZonedDateTime ObjectMapper가 인식하도록 수정 #704

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ default BooleanExpression eqMember() {
}

default BooleanExpression eqRecruitmentRound() {
return order.recruitmentRoundId.eq(recruitment.id);
return order.recruitmentRoundId.eq(recruitmentRound.id);
}

// TODO: MemberQueryMethod가 interface로 변경된 경우 해당 메서드 제거 및 대체
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class RegexConstant {
public static final String DEPARTMENT = "^D[0-9]{3}$";
public static final String HONGIK_EMAIL = "^[^\\W&=+'-+,<>]+(\\.[^\\W&=+'-+,<>]+)*@g\\.hongik\\.ac\\.kr$";
public static final String DATETIME = "yyyy-MM-dd'T'HH:mm:ss";
public static final String ZONED_DATETIME = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
public static final String DATE = "yyyy-MM-dd";
public static final String ACADEMIC_YEAR = "^[0-9]{4}$";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.gdschongik.gdsc.global.common.constant.RegexConstant.DATE;
import static com.gdschongik.gdsc.global.common.constant.RegexConstant.DATETIME;
import static com.gdschongik.gdsc.global.common.constant.RegexConstant.ZONED_DATETIME;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
Expand All @@ -11,11 +12,13 @@
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;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -25,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 @@ -39,6 +42,10 @@ public ObjectMapper objectMapper() {
module.addSerializer(LocalTime.class, new LocalTimeSerializer());
module.addDeserializer(LocalTime.class, new LocalTimeDeserializer());

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

mapper.registerModule(module);
return mapper;
}
Expand Down Expand Up @@ -107,4 +114,23 @@ public LocalTime deserialize(JsonParser jsonParser, DeserializationContext deser
return LocalTime.of(hour, minute, second, nano);
}
}

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)
throws IOException {
return ZonedDateTime.parse(
jsonParser.getValueAsString(),
DateTimeFormatter.ofPattern(ZONED_DATETIME).withZone(ZoneId.of("Asia/Seoul")));
}
}
}