Just Do IT!
[Error] Name for argument of type, @PathVariable name 생략 에러 본문
728x90
반응형
문제집 삭제 기능을 포스트맨으로 테스트 해보려고 하는데
2024-10-22T10:38:04.046+09:00 ERROR 9084 --- [test-dodream-backend] [nio-8080-exec-5] c.d.t.common.exception.ErrorController : Name for argument of type [java.lang.Long] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag. 2024-10-22T10:38:04.086+09:00 WARN 9084 --- [test-dodream-backend] [nio-8080-exec-5] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [java.lang.IllegalArgumentException: Name for argument of type [java.lang.Long] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.]
이런 오류가 났다.
좀 길기는 하지만 제대로 확인해보면 Spring MVC가 URL 경로 변수의 이름을 자동으로 인식하지 못해 발생하는 오류이다.
스프링 부트 3.2부터 매개변수의 이름을 인식하지 못하는 문제가 있다고 한다.
스프링 부트 3.2부터 자바 컴파일러에 -parameters 옵션을 넣어주어야 어노테이션의 이름을 생략할 수 있다.
스프링 부트에서 자동으로 파라미터 이름을 추론했을 때, 예상치 못한 결과를 가져오는 등의 문제를 해결하기 위해 스프링 부트 3.2 이후 버전에서는 파라미터 이름을 자동으로 추론하지 않도록 변경되었다.
이전에는 계속 이름을 넣어주었는데 이번에는 컨트롤러를 작성할 때 추가하지 않은 게 문제였다.
주로
@RequestParam, @PathVariable, @Autowired, @ConfigurationProperties
이 어노테이션들을 사용할 때 주의해야 한다.
변경한 코드
@DeleteMapping("/{id}")
public ResponseEntity<BookResponse> deleteBook(@AuthenticationPrincipal User user, @PathVariable("id") Long id) {
bookService.deleteBook(id, user);
return ResponseEntity.noContent().build(); // 204 No Content
}
@PathVariable에 제대로 이름을 넣어야 한다.
원래 다 넣어서 했었는데 한 번 까먹은 게 오류였다니...
다행히 id를 추가해주니 정상적으로 기능이 테스트되었다.
728x90