에러 메시지
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'todoController' method org.example.controller.TodoController#readAll() to {GET [/]}: There is already 'todoController' bean method org.example.controller.TodoController#readOne(Long) mapped.
여러 검색을 통해 얻은 결과
- 이 오류는 요청 매핑 핸들러 매핑(requestMappingHandlerMapping)을 초기화하는 동안 발생한 문제입니다.
- 해당 오류의 원인은 TodoController 클래스에 있는 readAll() 메서드와 readOne(Long) 메서드가 동일한 URL 패턴인 {GET [/]}에 매핑되어 충돌이 발생했다는 것을 알려줍니다.
- 이러한 충돌을 해결하기 위해서는 중복된 URL 매핑을 수정하거나, 다른 URL 패턴으로 변경해야 합니다.
에러 원인
@GetMapping
public ResponseEntity<TodoResponse> readOne(@PathVariable Long id) {
System.out.println("READ ONE");
TodoEntity result = this.service.searchById(id);
return ResponseEntity.ok(new TodoResponse(result));
}
@GetMapping
public ResponseEntity<List<TodoResponse>> readAll() {
System.out.println("READ ALL");
List<TodoEntity> list = this.service.searchAll();
List<TodoResponse> response = list.stream().map(TodoResponse::new)
.collect(Collectors.toList());
return ResponseEntity.ok(response);
}
위 코드에서 readAll 메소드에 id값을 불러오지 않는 건 당연하다. 모두 조회를 하기 때문.
그러나 readOne 메소드에 @PathVariable Long id(경로 변수)를 붙여놓고 id값을 받아오지 않았다.....
아래는 해결 후 코드
@GetMapping("{id}")
public ResponseEntity<TodoResponse> readOne(@PathVariable Long id) {
System.out.println("READ ONE");
TodoEntity result = this.service.searchById(id);
return ResponseEntity.ok(new TodoResponse(result));
}
@GetMapping
public ResponseEntity<List<TodoResponse>> readAll() {
System.out.println("READ ALL");
List<TodoEntity> list = this.service.searchAll();
List<TodoResponse> response = list.stream().map(TodoResponse::new)
.collect(Collectors.toList());
return ResponseEntity.ok(response);
}
'에러 기록' 카테고리의 다른 글
스웨거 Failed to load remote configuration (0) | 2024.01.10 |
---|---|
Swagger 403 에러 (0) | 2024.01.03 |
h2 console 접속 오류 : localhost에서 연결을 거부했습니다. (0) | 2023.12.29 |
AWS - cd 배포 과정 중 Environment still has health 오류 해결 (0) | 2023.12.27 |
MalformedJwtException - jwt토큰 손상 문제 (Swagger) (1) | 2023.11.16 |