[Moment.js] moment String을 Controller에서 LocalDateTime으로 받기
moment으로 startDate를 포맷하여 startDate.format("YYYY-MM-DDTHH:mm:ss") ajax를 이용해 컨트롤러로 보냈다.
LocalDateTime 데이터타입이 당연히 받을 수 있을 줄 알았는데 typeMismatch.java.time.LocalDateTime 에러가 발생했다.
문제
1 | // request body |
클라이언트에서 보내는 데이터는 위와 같다
1 | Field error in object 'calendarParam' on field 'startDate': rejected value [2023-10-01'T'00:00:00.000]; codes [typeMismatch.calendarParam.startDate,typeMismatch.startDate,typeMismatch.java.time.LocalDateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [calendarParam.startDate,startDate]; arguments []; default message [startDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDateTime' for property 'startDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value '2023-10-01'T'00:00:00.000'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2023-10-01'T'00:00:00.000]] |
typeMismatch 에러가 발생했다.
혹시 데이터포맷상 startDate.format("YYYY-MM-DDTHH:mm:ss")의 T 때문인가 싶어(그럴리없겠지만) 여러가지를 시도했다.
1 | startDate.format("YYYY-MM-DD'T'HH:mm:ss") //작은따옴표로 감싸보기 |
결과는 여전히 typeMismatch에러였다.
해결: String
LocalDateTime을 쓰고 싶었던 이유는 mybatis에서 시작일과 종료일 날짜연산을 하기위해서였다.
LocalDateTime을 String으로 변경하고 나서야 정상 작동했다. String이라도 형식이 맞기때문인지 날짜연산도 잘되었다.
해결!
![[Moment.js] moment String을 Controller에서 LocalDateTime으로 받기](https://github.com/voodootikigod/logo.js/raw/master/js.png)
![[OS/WINDOW]배포후 서버재시작에 batch와 윈도우 스케줄러 활용하기](https://cdn.pixabay.com/photo/2012/03/04/00/50/board-22098_960_720.jpg)
![[블로그]헥소테마에서 댓글기능 facebook에서 utterances로 변경하기](https://miro.medium.com/max/1600/1*aOv6h3h_v9PQWa03zGACnw.png)