본문 바로가기

Spring

[Spring Boot/오류] 2.3 버전 이후 매개변수의 이름을 인식하지 못하는 문제 -> Gradle을 사용하여 해결

☝️ 사용 버전

Spring Boot 3.2.5
JDK 17

 

 

 

💡 문제 상황

주문 조회를 했더니, 400 에러와 함께 아래와 같은 에러메세지가 떴다.

"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."

 

💡문제 코드

package org.store.clothstar.order.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.security.PermitAll;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.store.clothstar.common.dto.MessageDTO;
import org.store.clothstar.order.dto.reponse.OrderResponse;
import org.store.clothstar.order.dto.request.OrderRequestWrapper;
import org.store.clothstar.order.service.OrderApplicationService;
import org.store.clothstar.order.service.OrderService;
import org.store.clothstar.order.utils.URIBuilder;

import java.net.URI;


@Tag(name = "Order", description = "주문(Order) 정보 관리에 대한 API 입니다.")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/orders")
public class OrderController {

    private final OrderService orderService;
    private final OrderApplicationService orderApplicationService;

    @Operation(summary = "단일 주문 조회", description = "단일 주문의 정보를 조회한다.")
    @GetMapping("/{orderId}")
    public ResponseEntity<OrderResponse> getOrder(@PathVariable Long orderId) {

        OrderResponse orderResponse = orderService.getOrder(orderId);

        return ResponseEntity.ok().body(orderResponse);
    }

 

 

 

🔍 문제 원인

Spring Boot 3.2 버전부터는 자바 컴파일러가 메서드 파라미터의 이름을 제대로 인식하지 못하는 문제가 생긴다.

 

주로 아래의 어노테이션을 사용할 때 문제가 발생한다.

@RequestParam, @PathVariable, @Autowired, @ConfigurationProperties

 

 

- @PathVariable 관련

어노테이션에 "userId"라는 이름이 명확하게 있다. 문제 없이 작동한다.

public String mappingPath(@PathVariable("userId") String userId) {
    ...
}

 

어노테이션에 이름이 없다. -parameters 옵션 필요

public String mappingPath(@PathVariable String userId) {
  ...
}

 

 

문제 원인 관련 공식 링크:

https://github.com/spring-projects/spring-framework/wiki/Upgrading-to-Spring-Framework-6.x#parameter-name-retention

 

Upgrading to Spring Framework 6.x

Spring Framework. Contribute to spring-projects/spring-framework development by creating an account on GitHub.

github.com

 

 

 

🛠️ 문제 해결

해결 방안1

어노테이션에 이름을 생략하지 않고 다음과 같이 항상 적어준다.

@RequestParam("username") String username
@PathVariable("userId") String userId
@Qualifier("memberRepository") MemberRepository memberRepository

 

해결 방안2

컴파일 시점에 -parameters 옵션 적용

1. IntelliJ IDEA에서 File -> Settings를 연다. (Mac은 IntelliJ IDEA -> Settings)
2. Build, Execution, Deployment → Compiler → Java Compiler로 이동한다.
3. Additional command line parameters라는 항목에 다음을 추가한다.
-parameters


4. out 폴더를 삭제하고 다시 실행한다. 꼭 out 폴더를 삭제해야 다시 컴파일이 일어난다.

 

 

해결 방안3(권장)

Gradle을 사용해서 빌드하고 실행한다.

 

참고로 이 문제는 Build, Execution, Deployment -> Build Tools -> Gradle에서
Build and run using를 IntelliJ IDEA로 선택한 경우에만 발생한다. Gradle로 선택한 경우에는 Gradle이 컴파일 시점에 해당 옵션을 자동으로 적용해준다.

 

3번을 권장하는 이유는 코드 변경없이 문제를 해결할 수 있기 때문이다.

그리고 어노테이션의 이름을 생략함으로써 가독성을 높일 수 있다.

 

 

⭐ 결과 확인

1, 2, 3번 해결 방안을 각각 시도해본 결과, 정상적인 주문 조회가 가능했다.

 


Reference