Language & Framework/Spring
[Spring] RequestParam vs. RequestPathVariable 비교
수미수
2023. 10. 7. 00:00
반응형
RequestParam
- HTTP GET 요청에 대해 매칭되는 request parameter 값이 자동으로 들어간다.
- Url 뒤에 붙는 파라미터 값을 가져올 때 사용
RequestPathVariable
- HTTP 요청에 대해 매칭되는 request paramter 값이 자동으로 들어간다.
- Uri에서 각 구분자에 들어오는 값을 처리해야 할때 사용
- http://localhost:8080/index/1 와 같으며, REST API 에서 값을 호출할 때 주로 많이 사용한다.
동시 사용
- @PathVariable와 @RequestParam을 동시에 사용 가능
- http://localhost:8080/user/1/invoices?date=20180910
- 위와 같을 경우, userId 및 ReqeustParam을 동시에 가져올 수 있다.
@GetMapping("/user/{userId}/invoices")
public List<Invoice> listUsersInvoices(@PathVariable("userId") int user,
@RequestParam(value = "date", required = false) Date dateOrNull) {
}
RequestBody
- 반드시 HTTP POST 요청에 대해서만 처리한다.
- HTTP POST 요청에 대해 reqeust body에 있는 request message에서 값을 얻어와 매칭한다.
- RequestData를 바로 Model 이나 클래스로 맵핑 한다.
- JSON 이나 XML 과 같은 데이터를 적절한 메시지 컨버터로 읽을 때 사용하거나, POJO 형태의 데이터 전체로 받는 경우에사용
반응형