Validation failed for object='joinForm'. Error count: 1
org.springframework.web.method.annotation.ModelAttributeMethodProcessor1:Validationfailedforargument[0]inpublicjava.lang.Stringcom.sideproject.healMingle.boundContext.member.controller.MemberController.join(com.sideproject.healMingle.boundContext.member.controller.MemberControllerJoinForm): [Field error in object 'joinForm' on field 'profileImg': rejected value [IT_Infratructure.png]; codes [typeMismatch.joinForm.profileImg,typeMismatch.profileImg,typeMismatch.org.springframework.web.multipart.MultipartFile,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [joinForm.profileImg,profileImg]; arguments []; default message [profileImg]]; default message [Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'; Cannot convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile': no matching editors or conversion strategy found]]
--- 에러 내용---
오류 주요 내용
- 필드 경로 : 'joinForm.profileImg'
- 거부된 값 : IT_Infratructure.png (문자열)
- 오류 코드 : typeMismatch
- 기본 메시지: Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.MultipartFile'
원인 분석
- 'ModelAttribute'를 이용하여 폼 데이터를 'joinForm'. 객체에 바인딩할 때 'profileImg'필드가 MultipartFile 타입으로 선언되었으나, 실제로는 문자열 값이 전달됨
- 파일 업로드를 위한 HTML 폼이 enctype="multipart/form-data" 속성으로 설정되지 않았을 수 있음, 이 속성이 없을 시 브라우저는 파일을 올바르게 업로드 하지 않고, 파일 필드의 이름을 문자열 값으로 서버에 전송
해결방법
- enctype="multipart/form-data" 추가하기
<form action="/your-endpoint" method="post" enctype="multipart/form-data">
<input type="file" name="profileImg"/>
<button type="submit">Submit</button>
</form>