[Spring]@PathVariable과 @RequestParam Annotation

최대 1 분 소요

@PathVariable

어떠한 URL 요청에 대해서 하나의 PathVariable 밖에 설정 할 수 없다.

@RequestMapping("/ttt/{test}")
@ResponseBody
public String pathableTest(@PathVariable("test") String test){
    return test;
}

// URL
http://localhost:8080/ttt/abcd
// 응답 결과
abcd

@RequestParam

4가지 파라미터를 설정 할 수 있다.

  • defaultValue : 값이 없을때 기본 값을 설정
  • name : 바인딩 할 파라미터 이름
  • value : 바인딩 하여 별칭으로 정할 값
  • required : 기본값은 true로 설정되어있고 따로 false로 설정해주지 않는이상 반드시 값이 전달되어야 한다.
@RequestMapping("/ttt")
@ResponseBody
public String requestParamTest(@RequestParam("test") String test){
    return test;
}

// URL
http://localhost:8080/ttt?test=abcd
// 응답 결과
abcd

태그:

업데이트:

댓글남기기