JPA로 데이터베이스 다루기 파트에서 에러가 발생하였다
배경
고객 관련 api 컨트롤러에서 고객 정보를 update하는 메서드를 테스트 하던 중 에러가 발생하였다
테스트코드
CustApiControllerTest.java
CustUpdateRequestDto requestDto = CustUpdateRequestDto.builder()
.phoneNumber(expectPhoneNumber)
.smsConsentType(expectType.getLabel())
.build();
String url = "http://localhost:" + port + "/api/v1/cust/" + updateId;
HttpEntity<CustUpdateRequestDto> requestEntity = new HttpEntity<>(requestDto);
//when
ResponseEntity<Long> responseEntity = restTemplate.exchange(url, HttpMethod.PUT, requestEntity, Long.class);
System.out.println("responseEntitiy Body" + responseEntity.getBody());
- restTemplate.exchange() 라인에서 에러가 발생했다
에러로그
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.
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.updateNamedValueInfo(AbstractNamedValueMethodArgumentResolver.java:186) ~[spring-web-6.1.14.jar:6.1.14]
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.getNamedValueInfo(AbstractNamedValueMethodArgumentResolver.java:161) ~[spring-web-6.1.14.jar:6.1.14]
원인
- 컨트롤러 메서드에서 매개변수 이름을 명시하지 않아서 발생하는 문제
- @RequestParam, @PathVariable 등의 애너테이션이 붙은 메서드 매개변수에 이름이 명시되지 않았거나 컴파일러가 매개변수 이름 정보를 유지하지 못해서 발생하는 것.
- RestTemplate 요청 시에도 해당 컨트롤러 메서드가 호출되기 때문에 발생
해결 방법
CustApiController.java
@PutMapping("/api/v1/cust/{id}")
public Long update(@PathVariable("id") Long id, @RequestBody CustUpdateRequestDto requestDto){
return jpaCustService.update(id, requestDto);
}
- 변경전 @PathVariable -> 변경후 @PathVariable("id")
- url로 받는 패스파라미터에 있는 매개변수를 @PathVariable 애너테이션에 명시했어야했다
'Spring' 카테고리의 다른 글
| Spring Cloud Gateway로 마이크로서비스 라우팅 시 발생하는 404 오류의 원인과 해결책 (0) | 2025.03.11 |
|---|---|
| Spring Cloud Config Client 설정 후 포트번호가 변경되지 않을 때 (0) | 2025.02.20 |
| 6. 의존관계 자동주입 (0) | 2025.01.28 |
| 5. 컴포넌트 스캔 (0) | 2025.01.28 |
| 4. 싱글톤 컨테이너 (0) | 2025.01.28 |