[Spring] @PathVariable에 대한 나름의 이해

2023. 12. 24. 19:48Spring

@PathVariable은 request url에 들어있는 값을 변수로 가지고 오고 싶을때 사용한다.

예를 들어, 다음을 보자.

@GetMapping("/download/{contentId}")
    public ResponseEntity<Resource> downloadFile(@PathVariable("contentId") Long contentId) throws IOException {

내가 변수로 설정하고 싶은 부분을 request url에서 {name}으로 담는다.

그리고 메소드 파라미터에 @PathVariable("name") type variable을 넣어주면 name의 값을 variable에 값이 담겨 이를 사용할 수 있다.