study를 조회할 때 @PathVariable로 studyId로 조회하는 것과 @RequestParam으로 participantCode로 조회하는 것을 묶어보았다.

    @GetMapping("/api/studies/{studyId}")
    public ResponseEntity<PomodoroRoomResponse> findStudy(
            @PathVariable(required = false) Long studyId,
            @RequestParam(required = false) String participantCode
    ) {
        PomodoroRoomResponse pomodoroRoom = pomodoroRoomService.findPomodoroRoomWithFilter(studyId,
                participantCode);
        return ResponseEntity.ok(pomodoroRoom);
    }

 

 

 

 

 

@PathVariable과 @RequestParam 둘 다 require가 default ture이고 required = false로 설정하면 다음과 같이 두 요청 모두 핸들링이 가능하다고 생각했다.

- /api/stuides/{studyId}

- /api/studies?participantCode

하지만 @PathVVariable의 required를 false를 하더라도 다음과 같이 예상한대로 작동하지는 않는다.

@PathVariable에 required를 false를 하더라도 /api/stuides/{studyId} 형태가 아니면 핸들러 맵핑이 안되는 것.

이러한 경우는 핸들러를 분리하여 따로 작성해야 한다.

+ Recent posts