[SpringBoot/Thymeleaf] java.lang.NumberFormatException : Failed to convert value of type 'java.lang.String' to required type 'int'; For input string: "{idx}"

삽질/트러블슈팅 2023. 5. 21. 05:13
728x90
728x90

에러 메세지

Failed to convert value of type 'java.lang.String' to required type 'int'; For input string: "{idx}"

 

 

 

 

에러 원인

AWS S3로 이미지 업로드를 구현하는 과정에서 발생했고

 

String형태로 넘어가서 int로 받지를 못하는 듯 하다

@ResponseBody
@PostMapping("/img/{idx}")
public String img_modify(@PathVariable int idx, @RequestParam("imgfile") MultipartFile imgfile, UserDTO dto) throws IOException {
    System.out.println(imgfile.getName());
    System.out.println(imgfile.getSize());
    System.out.println(imgfile.getOriginalFilename());

    System.out.println(dto);
    if(userService.img_modify(imgfile, dto)) return "성공";
    else return "실패";
}
<form method="post" action="/users/img/{idx}" name="imgchange" enctype="multipart/form-data">
    <input type="file" name="imgfile" style="display : none;">
    <input type="hidden" name="idx" th:text="${idx}">		
    <input type="hidden" name="img">
</form>

 

 

 

해결

action에서 idx값을 받아주지 못했음 - 타임리프 문법으로 변경

<form th:method="post" th:action="@{|/users/img/${idx}|}" name="imgchange" enctype="multipart/form-data">
    <input type="file" name="imgfile" style="display : none;">
    <input type="hidden" name="idx" th:value="${idx}">		
    <input type="hidden" name="img">
</form>
728x90
300x250
mag1c

mag1c

2년차 주니어 개발자.

방명록