🚨 Name for argument of type [int] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.
java.lang.IllegalArgumentException: Name for argument of type [int] not specified
Ensure that the compiler uses the '-parameters' flag.
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Feb 19 13:33:40 KST 2025
There was an unexpected error (type=Internal Server Error, status=500).
Name for argument of type [int] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.
java.lang.IllegalArgumentException: Name for argument of type [int] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.
🔥 오류 원인
@PathVariable이 경로 변수 이름을 올바르게 매핑하지 못할 때 발생하는 에러!!
📖 에러 코드 (수정 전) - 근데 잘 못된게 없다..!! 해결방법 2번 참고.
@RestController
@RequestMapping("/api/query/products")
@RequiredArgsConstructor
public class ProductQueryController {
private final ProductQueryService productQueryService;
// 조회 (상품 목록, 상세 조회 등)
@GetMapping("/{id}")
public ProductDetailDTO getProductDetail(@PathVariable int id) {
return productQueryService.getProductDetail(id);
}
}
🛠 해결 방법
1️⃣ -parameters 컴파일 옵션 설정
2️⃣ @PathVariable("productId")와 URL 경로 변수를 정확히 맞추기
➡️⭐ @PathVariable("productId")를 명시적으로 사용 (내 경우 IDE에서 생략해도 된다고 나오는데 생략하면 위 오류가 났다!)
3️⃣ 매개변수 타입이 일치하는지 확인하기
✅ 1. -parameters 옵션 추가 (컴파일러 설정)
- JVM이 컴파일할 때 인자명을 유지하도록 설정해야 함.
- 아래 설정 후 로젝트를 mvn clean package 또는 ./gradlew build로 다시 빌드해야 반영됨.
📖 pom.xml (Maven 사용 시)
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source> <!-- 자바 버전에 맞게 설정 -->
<target>17</target>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
📖 build.gradle.kts (Gradle 사용 시)
tasks.withType<JavaCompile> {
options.compilerArgs.add("-parameters")
}
✅ 2. @PathVariable과 URL 경로 매칭 문제 해결
- @PathVariable의 변수명과 @GetMapping의 {} 경로 변수가 다르면 에러가 발생할 수 있음.
- @PathVariable("productId")가 {productId}와 정확히 일치해야 함.
- ➡️ 변수명을 생략하면 에러 발생 가능하므로 @PathVariable("id")를 명시적으로 사용
@RestController
@RequestMapping("/api/query/products")
public class ProductQueryController {
@GetMapping("/{id}") // 🔥 URL과 PathVariable 이름 일치!
public ResponseEntity<ProductDetailDTO> getProductById(@PathVariable("id") Long id) {
ProductDetailDTO product = productQueryService.getProductById(id);
return product != null ? ResponseEntity.ok(product) : ResponseEntity.notFound().build();
}
}
✅ 3. @PathVariable의 타입 일치 확인
- @PathVariable int productId인데, Long을 기대하면 타입 불일치 에러 발생
@GetMapping("/{id}")
public ResponseEntity<ProductDetailDTO> getProductById(@PathVariable Long id) {
ProductDetailDTO product = productQueryService.getProductById(id);
return ResponseEntity.ok(product);
}
'Backend > spring' 카테고리의 다른 글
환경 변수 관리 (@Value, @ConfigurationProperties) (0) | 2025.02.26 |
---|---|
[error] getOutputStream() has already been called for this response (0) | 2025.02.23 |
[JAVA] 쿠키(Cookie) 설정 방법 (0) | 2025.02.23 |
[Spring Security] CSRF & CORS 개념 및 설정 방법 (0) | 2025.02.23 |
[error] conversionServicePostProcessor Bean 중복 오류 (0) | 2025.02.23 |