[Spring] @PathVariable

null·2023년 3월 15일
0

Spring

목록 보기
4/7

@PathVariable("변수명")

	@GetMapping("/board/{no}")
	public String viewBoard(@PathVariable("no") int no, Model model) throws Exception {

		model.addAttribute("viewBoard", boardService.getBoardNo(no));
		return "board/detail";
	}
  • /board/{no} 와 같은 방식으로 매핑
    - http://localhost:8888/board/275
    - @PathVariable 적용 전 : http://localhost:8888/board/?no=275

  • @PathVariable 어노테이션의 변수 이름은 일치해야 한다

  • URL 경로의 일부를 컨트롤러 메서드의 인수로 바인딩하는 데 사용

  • Rest API에서 값을 호출할 때 주로 많이 사용

  • 해당 정보(no = 275)를 얻기 위한 코드 작성 필요 없어짐 -> 코드량 감소, 실수 확률 감소 등 유지보수성 향상

  • no(275,276 ...) 값에 따라 다른 리소스를 반환할 수 있다 : 이렇게 URL 경로의 일부를 동적으로 생성함으로써, 다양한 리소스를 처리할 수 있는 RESTful API를 구현할 수 있다

0개의 댓글