- Request 파라미터
Client가 Server에 요청(Request)할 때 추가적으로 전송하는 데이터
- 유형
- Query String
- Path Parameter
+ 대용량 데이터의 경우 - Request Body
1. Query String
URI 뒤에 ?key1=value1&key2=value2&... 형태로 작성
ex)
http://localhost:8080/post?category=it&id=10
https://news.naver.com/main/main.naver?mode=LSD&mid=shm&sid1=105
메서드 파라미터에 @RequestParam Annotaion 사용
name 요소는 Query String의 key에 매핑되고 value 값은 메서드 파라미터에 저장된다.
http://localhost:8080/post?category=it&id=10
-> category에 it, id에 10이 저장된다.
package com.example.controllerexercise.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RequestParamController {
// getPost 메소드 호출 위한 api uri는 /post이고
// 2개의 query string을 request parameter로 받는다.
@RequestMapping(value = "/post")
public String getPost(@RequestParam(name = "category") String category,
@RequestParam(name = "id") Integer id) {
return "You requested " + category + " - " + id + " post";
}
}
- @RequestParam 요소
- name: Query String의 key 이름. Query String의 key와 변수명이 같을 경우 생략 가능하다.
- required: 필수 여부 (default = true)
- defaultValue: 데이터가 없을 경우 기본값 (전송된 값이 있을 경우 전송된 값을 사용한다. )
@RequestMapping(value = "/post")
public String getPost(@RequestParam(name = "category",
required = false,
defaultValue = "it") String category,
@RequestParam Integer id //name 요소 생략
@RequestParam(name = "num") Integer number { //다른 경우 생략x (num != number)
return "You requested " + category + " - " + id + " post";
}
* Query String을 지정해놨는데 데이터를 하나라도 보내지 않으면 에러가 발생한다.
There was an unexpected error (type=Bad Request, status=400).
* defaultValue를 정의하지 않고 required = false로 설정한 뒤 값을 보내지 않으면 null로 표시된다. (String, Integer 모두)
2. Path Parameter
URI의 일부로 파라미터 값을 사용한다.
ex)
도메인/user/3/post/5
https://www.codepresso.kr/course/9
@RequestMapping value URI에 {}로 Path Param임으로 표시한다.
메서드 파라미터에 @PathVariable Annotation을 사용한다.
중괄호로 묶인 영역에 전달되는 데이터는 메서드의 파라미터에 각각 저장된다.
http://localhost:8080/user/admin/id/10
-> type에 admin이, id에 10이 저장된다.
@RequestMapping(value = "/user/{type}/id/{id}")
public String getUser(@PathVariable(name = "type") String type,
@PathVariable(name = "id") Integer id) {
return "You requested " + type + " - " + id + " user";
}
3. Query String vs Path Parameter
특정 자원을 요청하는 경우 Path Param을, 정렬이나 추가 필터링을 위한 데이터는 Query String을 사용한다.
URI 일부로 파라미터 값을 사용하는 구조 상, 선택적 데이터의 경우 Path Param을 잘 사용하지 않는다.
따라서 필수 데이터는 Path Param을, 선택적 데이터는 Query String을 사용한다.
4. 대용량 데이터의 경우 - Request Body
게시판 글, 작성된 form 데이터 등 대용량 데이터를 보내기 위해서는 다른 방식이 필요하다.
GET, DELETE Method는 대용량 데이터를 전송하는 경우가 많지 않아 Query String, Path Param을 주로 사용한다.
일반적으로 데이터를 저장 및 수정하는 POST와 PUT Method에서 Request Body를 사용한다.
Request Body에 다양한 포맷의 데이터 전송이 가능하나 주로 JSON 데이터 형식을 사용한다.
Client에서 JSON 데이터를 전송하고 Spring에서는 JSON 데이터를 Java 객체 파라미터로 저장한다.
'BackEnd > spring' 카테고리의 다른 글
| [Spring Boot] Spring Service (0) | 2023.06.14 |
|---|---|
| [Spring Boot] HTTP Method와 데이터베이스 CRUD (0) | 2023.06.09 |
| [Spring Boot] @RequestMapping과 URI (0) | 2023.06.07 |
| [Spring Boot] Spring Controller와 Annotaion / @Controller와 @RestController 차이 (0) | 2023.06.07 |
| [Spring Boot] SW 아키텍처 - 계층형 아키텍처 패턴 Layered Architecture (0) | 2023.06.07 |