[Spring] 경로 데이터 받기 애너테이션

SeoHa·2025년 6월 19일

SPRING

목록 보기
4/7

스프링 - 경로 데이터 받기 애너테이션


@RequestParam : 쿼리 파라미터나 일반 form 데이터 받을 때 (?state=예약중)

  • mapping 경로 : @Get/Post Mapping()
  • 경로에 해당 파라미터 값들이 자동으로 들어감
  • 속성으로 변수명을 지정할 경우 해당 변수명으로 값을 받음
  • 속성으로 required를 넣으면 값의 필수 유무를 지정
    (true -> 필수 / false -> 필수 아님)

<예시>

@RequestMapping("/reservations")
@GetMapping
public BaseResponse<ReservationSearchResponse> findUserById(
			@RequestParam Boolean isDropper,
            @RequestParam(required = false) String state
    ) 

-> GET /reservations?isDropper=true&state=예약중

@RequestPart : multipart/form-data 요청에서 파일이나 JSON 객체 받을 때

  • mapping 경로 : @PostMapping(value = "/주소", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  • @RequestPart는 GET 요청이나 application/json 요청에서는 작동하지 않음

<예시>

@PostMapping(value = "/reservations", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> uploadReservation(
        @RequestPart ReservationRequest data,
        @RequestPart(required = false) MultipartFile file
)

@PathVariable : URL 경로의 일부분 받을 때 (/state/예약중)

  • mapping 경로 : @Get/Post Mapping("/{여기 안에 변수 들어감}")

<예시>

@RequestMapping("/locks")
@GetMapping("/{lockerId}")
 public BaseResponse<LockerDetailResponse> findUserById(@PathVariable Long lockerId) {

        return new BaseResponse<>(lockerService.findUserById(lockerId));
    }

-> GET /locks/1 (Id값이 들어옴)

0개의 댓글