728x90
반응형
SMALL
문제 발생
LocalDateTime 을 직렬화 하는 과정에서 해당 오류 발생
@Test
public void localDateTimeToJson() throws JsonProcessingException {
LocalDateTime target = LocalDateTime.of(2022,11,10,18,12);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(target);
log.info("{}",json);
}
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
문제 해결
예외구문에서 jsr310 의존성에 관한 이야기를 하고있음. 의존성 추가
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310 -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.14.0</version>
</dependency>
mapper를 사용하여 변환하는 코드 변경
mapper.registerModule(new JavaTimeModule()).writeValueAsString(target);
@Test
public void localDateTimeToJson() throws JsonProcessingException {
LocalDateTime target = LocalDateTime.of(2022,11,10,18,12);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.registerModule(new JavaTimeModule()).writeValueAsString(target);
log.info("result {}",json);
}
결과

성공적
728x90
반응형
LIST