@RequestParam @PathVariable 는 request URI로부터 values를 가져올때 사용한다.
@RequestParam은 query string을, @PathVariable 은 URI path에서 값을 가져온다.
/**
* @PathVariable
* uri path에서 값을 추출하기때문에 encoding X
*/
http://localhost:8080/spring-mvc-basics/foos/abc
-> ID: abc
http://localhost:8080/spring-mvc-basics/foos/ab+c
-> ID: ab+c
******************
@GetMapping("/foos/{id}")
@ResponseBody
public String getFooById(@PathVariable String id){
return "ID:" + id;
}
/**
* @RequestParam
* parameter가 decode 된다
*/
http://localhost:8080/spring-mvc-basics/foos?id=abc
-> ID: abc
http://localhost:8080/spring-mvc-basics/foos?id=ab+c
-> ID: ab c
******************
@GetMapping("/foos")
@ResponseBody
public String getFooById(@RequestParam String id){
return "ID:" + id;
}
https://ryan-han.com/post/translated/pathvariable_queryparam/
https://www.baeldung.com/spring-requestparam-vs-pathvariable
반응형
'Spring MVC' 카테고리의 다른 글
Spring Boot Upgrade 2.0.x -> 2.7.2 (0) | 2022.07.27 |
---|---|
MyBatis Package 경로 줄이기 (0) | 2022.05.19 |
Spring Batch (0) | 2021.12.21 |
Spring Batch (0) | 2019.01.18 |
Spring Transaction 관련 (0) | 2016.11.30 |