Framework/Spring Boot

[Error] Could not write JSON: Java 8 date/time type `java.time.LocalDateTime` not supported by default:

잔망루피 2022. 7. 16. 19:50
There was an unexpected error (type=Internal Server Error, status=500).
Could not write JSON: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: java.util.ArrayList[0]->Bulletin.Board.domain.posts.Post["createdDate"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: java.util.ArrayList[0]->Bulletin.Board.domain.posts.Post["createdDate"])
org.springframework.data.redis.serializer.SerializationException: Could not write JSON: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: java.util.ArrayList[0]->Bulletin.Board.domain.posts.Post["createdDate"]); nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: java.util.ArrayList[0]->Bulletin.Board.domain.posts.Post["createdDate"])
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: java.util.ArrayList[0]->Bulletin.Board.domain.posts.Post["createdDate"])

 

 

✨ 해결

implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.3'

먼저, 에러 로그에 나온대로 "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" 모듈을 dependency에 추가했다.

 

@JsonSerialize(using= LocalDateTimeSerializer.class)
@JsonDeserialize(using= LocalDateTimeDeserializer.class)
private LocalDateTime createdDate;

@JsonSerialize(using= LocalDateTimeSerializer.class)
@JsonDeserialize(using= LocalDateTimeDeserializer.class)
private LocalDateTime modifiedDate;

@JsonSerialize@JsonDeserialize 어노테이션을 붙였다.

@JsonSerialize using = 사용할 Serializer class 지정
getter 메소드 / 필드 / value classes 에 붙인다.
참고) value classes는 value objects라고도 한다. value classes는 값 집합을 가지고 있고, 로직이 거의 없거나 전혀 없는 객체다. DTO가 value object의 예다.
@JsonDeserialize using = 사용할 Deserializer class 지정
setter 메소드 / 필드 / value classes 에 붙인다.
LocalDateTimeSerializer Java 8 임시 LocalDateTimes의 Serializer
LocalDateTimeDeserializer Java 8 임시 LocalDateTimes의 Deserializer

 

 

참고 👇

https://www.quora.com/What-are-value-classes-in-Java

 

What are value classes in Java?

Answer (1 of 2): The more common term is value objects - it means objects that just hold a set of values, and contain little or no logic. Of course a value object must be an instance of a value class, but since there are (by definition) no logic and no cla

www.quora.com

 

https://fasterxml.github.io/jackson-databind/javadoc/2.9/com/fasterxml/jackson/databind/annotation/JsonSerialize.html

 

JsonSerialize (jackson-databind 2.9.0 API)

Whether type detection used is dynamic or static: that is, whether actual runtime type is used (dynamic), or just the declared type (static). Note that Jackson 2.3 changed default to DEFAULT_TYPING, which is roughly same as saying "whatever". This is impor

fasterxml.github.io

 

https://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/annotation/JsonDeserialize.html#using() 

 

JsonDeserialize (jackson-databind 2.8.0 API)

Concrete type to deserialize values as, instead of type otherwise declared. Must be a subtype of declared type; otherwise an exception may be thrown by deserializer. Bogus type Void can be used to indicate that declared type is used as is (i.e. this annota

fasterxml.github.io

 

https://fasterxml.github.io/jackson-modules-java8/javadoc/datetime/2.9.pr1/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateTimeSerializer.html

 

LocalDateTimeSerializer (Jackson datatype: JSR310 2.9.0.pr1 API)

void serializeWithType(T value, com.fasterxml.jackson.core.JsonGenerator generator, com.fasterxml.jackson.databind.SerializerProvider provider, com.fasterxml.jackson.databind.jsontype.TypeSerializer serializer) 

fasterxml.github.io

 

https://www.javadoc.io/doc/com.fasterxml.jackson.datatype/jackson-datatype-jsr310/2.8.5/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateTimeDeserializer.html

 

LocalDateTimeDeserializer - jackson-datatype-jsr310 2.8.5 javadoc

Latest version of com.fasterxml.jackson.datatype:jackson-datatype-jsr310 https://javadoc.io/doc/com.fasterxml.jackson.datatype/jackson-datatype-jsr310 Current version 2.8.5 https://javadoc.io/doc/com.fasterxml.jackson.datatype/jackson-datatype-jsr310/2.8.5

www.javadoc.io

 

반응형