Name for argument of type [java.lang.String] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.
spring 2025. 9. 20. 21:28
원인
자바는 기본적으로 컴파일하면 메소드 파라미터 이름을 클래스 파일에 저장하지 않음.
즉, public String hello(String name) 의 "name"이 런타임에는 알 수 없음 → 그냥 arg0 으로만 저장됨.
스프링은 @RequestParam, @PathVariable, @Param 같은 어노테이션이 없을 때, 파라미터 이름을 그대로 사용하려고 함.
그런데 이름이 없으니까 "이름을 알 수 없다"는 에러가 뜨는 것.
해결방법
1. 컴파일 옵션 추가 (추천)
javac 컴파일 시 -parameters 플래그를 추가하면,
메소드 파라미터 이름이 .class 파일에 저장돼서 리플렉션으로 읽을 수 있음.
Maven
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>
Gradle
tasks.withType(JavaCompile) {
options.compilerArgs << "-parameters"
}
2. 어노테이션으로 직접 지정
스프링에서 매개변수에 어노테이션을 붙여서 이름을 명시하면 해결됨.
@GetMapping("/hello")
public String hello(@RequestParam("name") String name) {
return "Hello " + name;
}
또는 Spring Data JPA에서
@Query("SELECT u FROM User u WHERE u.username = :username")
User findByUsername(@Param("username") String username);
'spring' 카테고리의 다른 글
spring + hibernate enum 타입 문제 해결 (0) | 2024.01.14 |
---|---|
Spring Profiles (0) | 2022.12.08 |
스프링 관련 블로그 (0) | 2022.12.05 |
springframework java configuration (0) | 2022.10.07 |
@Conditional (0) | 2022.08.23 |