Web Programming/Spring

Spring Request Mapping 매개변수 오류

럼포 2024. 12. 20. 13:37

문제상황

 

put request를 진행 중 다음과 같은 오류가 떴다. 

java.lang.IllegalArgumentException: Name for argument of type [java.lang.Integer] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.

 

해결방법

1. compiler옵션에 -parameters 추가

(intellij) 사용 시

File -> settings -> Build, Execution, Deployment → Compiler → Java Compiler 에서 Additional command line parameters에 다음 항목 추가

- parameters

프로젝트 폴더 중 out 폴더를 삭제하고 다시 실행(out 폴더를 삭제해야 다시 컴파일 된다고 함.)

 

2. 파라미터 이름을 명확하게 명시

@PutMapping("/{postId}")
    public void updatePost(@PathVariable("postId") Integer postId, @RequestBody Post post){
        postService.update(postId, post);
    }

 

@PathVariable에 전달하고자 하는 파라미터 이름을 지정해준다.

 

 

 

참고:

https://velog.io/@ghwns9991/%EC%8A%A4%ED%94%84%EB%A7%81-%EB%B6%80%ED%8A%B8-3.2-%EB%A7%A4%EA%B0%9C%EB%B3%80%EC%88%98-%EC%9D%B4%EB%A6%84-%EC%9D%B8%EC%8B%9D-%EB%AC%B8%EC%A0%9C