본문 바로가기

개발/Spring

스프링 REST API - @PathVariable

728x90
반응형
@PathVariable 이란?

 

REST API 에서 URI 에 변수가 들어가는 것을 볼 수 있습니다.

@PathVariable 을 사용하면 브라우저에서 요청 URL 로 전달된 매개변수를 가져올 수 있습니다.

 

ex) http://bookclub.yes24.com/BookClub/Detail/106494677 

 

위의 굵은 글씨로 되어 있는 부분을 @PathVariable 로 처리해줄 수 있습니다.

 

...
@RestController
@RequestMapping("/test/*")
public class TestController {
	static Logger logger = LoggerFactory.getLogger(TestController.class);

	// 브라우저에서 요청 시 {num} 부분의 값이 @PathVariable 로 지정
	@RequestMapping(value = "/notice/{num}", method = RequestMethod.GET)
	// 요청 URL 에서 지정된 값이 num 에 자동으로 할당
	public int notice(@PathVariable("num") int num) throws Exception {
		return num;
	}
...

 

 

여기까지 임미다.

728x90