[Spring] Spring MVC에서 Date 타입 변환 오류

2021. 7. 27. 22:12스프링

SMALL

MySQL 메뉴얼 사이트 : https://dev.mysql.com/doc/refman/8.0/

 

게시글을 수정하는 도중 Date의 매핑에 실패합니다.

 

WARN : org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'boardVO' on field 'regDate': rejected value [2021-07-27]; codes [typeMismatch.boardVO.regDate,typeMismatch.regDate,typeMismatch.java.sql.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [boardVO.regDate,regDate]; arguments []; default message [regDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Date' for property 'regDate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.sql.Date' for property 'regDate': no matching editors or conversion strategy found]]

 

스프링에서는 기본적으로 정수, 문자열, 불린 타입의 기본 변환은 자동으로 해준다고 합니다.

하지만 날짜의 경우는 변환해줘야 한다고 합니다.

1. 어노테이션을 이용할 경우ㄱ

#실패#

실패.. (결국 String으로 사용)

 

2021-08-06자

MySQL에서 regDate 테이블의 속성을 다음과 같이 변경해서 오류가 사라졌습니다.

ALTER TABLE Board MODIFY regDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

위 어노테이션 설정을 하지 않는 경우 => Fri Aug 06 15:38:35 KST 2021 모양으로 저장됩니다.

LIST