본문 바로가기
공부/Trouble Shooting

시간을 인자로 받을 때 [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: raw timestamp (2023) not allowed for `java.time.LocalDateTime`: n..

by son_i 2023. 10. 25.
728x90

클라이언트로부터 예약시간을 받을 ReserveDTO를 만들었다.

package com.soni.reservation.dto;


import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.*;
import org.springframework.data.annotation.CreatedDate;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ReserveDto {

        private String storeName;

        private LocalDateTime reservedAt;
}

이러고 PostMan을 활용해 받으려고 하니 오류가 났다.

[nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: raw timestamp (2023) not allowed for `java.time.LocalDateTime`: need additional information such as an offset or time-zone (see class Javadocs);

구글링을 해보니까 @JsonFormat을 사용하면 된다는 사실을 알았다.

@DateTimeFormat은 안되고 @JsonFormat은 되는 이유 ?

 

Jackson : Json Data 파싱을 돕는 라이브러리

 

컨트롤러가 데이터를 받을 때 @RequestBody를 통해 json형태로 받는다.

클라이언트로부터 JSON 문자열을 받으면 스프링부트는 이것을 HttpMessageConverter에 넘겨서

json -> 객체 형태로 변환 (역직렬화)한다. 이 과정에서 jackson 라이브러리가 사용된다.

 

@DateTimeFormat은 Spring framework에 종속되어있다. 스프링부트에서는 파싱을 하기 위해 Jsckson 라이브러리를 알아야하는데 Jackson은 Spring을 알지 못 한다. 그래서 오류가 나는 것  !

 

날짜타입의 데이터를 클라이언트 -> 서버로 받을 때 String으로 받아서 따로 변환하지 않아줘도 되고 어노테이션을 이용하면 바로 가능함.

 

package com.soni.reservation.dto;


import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.*;
import org.springframework.data.annotation.CreatedDate;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.time.LocalDateTime;

@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ReserveDto {

        private String storeName;

        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss a", timezone = "Asia/Seoul")
        private LocalDateTime reservedAt;
}

 

 

 

------

참조

https://woonys.tistory.com/entry/Spring-Controller%EC%97%90%EC%84%9C-String-%EB%82%A0%EC%A7%9C-%ED%83%80%EC%9E%85-%EC%9E%90%EB%8F%99%EC%9C%BC%EB%A1%9C-%EB%B3%80%ED%99%98%ED%95%98%EA%B8%B0Feat-DateTimeFormat-%EC%A0%81%EC%9A%A9-%EC%95%88%EB%90%98%EB%8A%94-%EC%9D%B4%EC%9C%A0-JAVA%EC%97%90%EC%84%9C-JSON%EC%9D%84-%EB%B3%80%ED%99%98%ED%95%98%EB%8A%94-%EA%B3%BC%EC%A0%95

 

[Spring] Controller에서 String -> 날짜 타입 자동으로 변환하기(Feat. @DateTimeFormat 적용 안되는 이유 & JAVA

Introduction 모든 예제는 github에 올려뒀습니다 예전에 했던 작업에서 클라이언트로부터 날짜값을 받는데 쓰는 requestDTO를 만들었다. 날짜값이 클라이언트로부터 String 타입으로 들어오기에 requestDTO

woonys.tistory.com