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

Java 8 date/time type 'java.time.LocalDateTime' not supported by default #9

Open
yunyoung1819 opened this issue Jul 14, 2023 · 0 comments
Assignees
Labels
bug Something isn't working Redis

Comments

@yunyoung1819
Copy link
Owner

yunyoung1819 commented Jul 14, 2023

상황

  • Redis Caching 적용 시 아래와 같은 에러가 발생함
Java 8 date/time type `java.time.LocalDateTime not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310"

원인

  • LocalDateTime을 역직렬화하지 못해서 발생하는 문제
  • Redis 캐싱 처리할 때 직렬화 또는 역직렬화를 이용해 데이터 저장과 조회를 하기 때문

해결

  1. jackson-datatype-jsr310 종속성을 추가
// 자바 역직렬화 문제 해결 패키지
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
implementation 'com.fasterxml.jackson.core:jackson-databind'
  1. 캐시로 사용할 객체에 LocalDateTime 타입의 값이 존재한다면 @JsonSerialize, @JsonDeserialize 애노테이션을 기입해주어야 함
public record ItemsResponseDto(List<ItemDetailDto> items) {
        public record ItemDetailDto(
                @Schema(description = "동별 드랍 아이템 개수", example = "1")
                Long itemId,

                @Schema(description = "사용자 정보")
                UserResponseDto user,

                @Schema(description = "사용자 위치", example = "성동구 성수1가 1동")
                ItemLocationResponseDto location,

                @Schema(description = "음악 정보")
                MusicResponseDto music,

                @Schema(description = "사용자 코멘트", example = "이 노래 좋아요")
                String content,

                @Schema(description = "생성시간", example = "yyyy-MM-dd HH:mm:ss")
                @JsonFormat(
                        shape = JsonFormat.Shape.STRING,
                        pattern = "yyyy-MM-dd HH:mm:ss",
                        locale = "Asia/Seoul"
                )
                @JsonSerialize(using = LocalDateTimeSerializer.class)
                @JsonDeserialize(using =LocalDateTimeDeserializer.class)
                LocalDateTime createdAt,

                @Schema(description = "아이템 좋아요 여부", example = "true")
                boolean isLiked,

                @Schema(description = "아이템 좋아요 개수", example = "100")
                int itemLikeCount
        ) {
                public ItemDetailDto(Item item) {
                        this(
                                item.getId(),
                                new UserResponseDto(item.getUser()),
                                new ItemLocationResponseDto(item.getItemLocation().getName()),
                                new MusicResponseDto(item),
                                item.getContent(),
                                item.getCreatedAt(),
                                item.isLiked(item.getUser()),
                                item.getItemLikeCount()
                        );
                }
        }
}

참고

@yunyoung1819 yunyoung1819 added bug Something isn't working Redis labels Jul 14, 2023
@yunyoung1819 yunyoung1819 self-assigned this Jul 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Redis
Projects
None yet
Development

No branches or pull requests

1 participant